开发随笔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  25 随笔 :: 2 文章 :: 2 评论 :: 22318 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1、获得当前运行程序的路径
string rootPath = Directory.GetCurrentDirectory();<br>
2、获得该文件夹下的文件,返回类型为FileInfo
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] files=root.GetFiles();<br>
3、获得该文件夹下的子目录,返回类型为DirectoryInfo
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
DirctoryInfo[] dics=root.GetDirectories();<br>
4、获得文件夹名
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
string dicName=root.Name;<br>
5、获得文件夹完整的路径名
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
string dicName=root.FullName;<br>
6、获取文件的Name和FullName
string path=@"X:\XXX\XX";
DirectoryInfo root = new DirectoryInfo(path);
foreach (FileInfo f in root.GetFiles())
{
    string name=f.Name;
    string fullName=f.FullName;
} <br><br><br>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
7、只获取目录下一级的文件夹与文件
String path = @"X:\xxx\xxx";
 
//第一种方法
string[] files = Directory.GetFiles(path, "*.txt");
foreach (string file in files)
{
   Console.WriteLine(file);
}
//第二种方法
DirectoryInfo folder = new DirectoryInfo(path);
foreach (FileInfo file in folder.GetFiles("*.txt"))
{
     Console.WriteLine(file.FullName);
}
 
8、递归地输出当前运行程序所在的磁盘下的所有文件名和子目录名
static void Main(string[] args)
{
    //获取当前程序所在的文件路径
    String rootPath = Directory.GetCurrentDirectory();
    string parentPath = Directory.GetParent(rootPath).FullName;//上级目录
    string topPath = Directory.GetParent(parentPath).FullName;//上上级目录
    StreamWriter sw = null;
    try
    {
        //创建输出流,将得到文件名子目录名保存到txt中
        sw = new StreamWriter(new FileStream("fileList.txt", FileMode.Append));
        sw.WriteLine("根目录:" + topPath);
        getDirectory(sw, topPath, 2);
    }
    catch (IOException e)
    {
        Console.WriteLine(e.Message);
    }
    finally
    {
        if (sw != null)
        {
            sw.Close();
            Console.WriteLine("完成");
        }
    }
 
}
 
/// <summary>
/// 获得指定路径下所有文件名
/// </summary>
/// <param name="sw">文件写入流</param>
/// <param name="path">文件写入流</param>
/// <param name="indent">输出时的缩进量</param>
public static void getFileName(StreamWriter sw, string path, int indent)
{
    DirectoryInfo root = new DirectoryInfo(path);
    foreach (FileInfo f in root.GetFiles())
    {
        for (int i = 0; i < indent; i++)
        {
            sw.Write("  ");
        }
        sw.WriteLine(f.Name);
    }
}
 
/// <summary>
/// 获得指定路径下所有子目录名
/// </summary>
/// <param name="sw">文件写入流</param>
/// <param name="path">文件夹路径</param>
/// <param name="indent">输出时的缩进量</param>
public static void getDirectory(StreamWriter sw, string path, int indent)
{
    getFileName(sw, path, indent);
    DirectoryInfo root = new DirectoryInfo(path);
    foreach (DirectoryInfo d in root.GetDirectories())
    {
        for (int i = 0; i < indent; i++)
        {
            sw.Write("  ");
        }
        sw.WriteLine("文件夹:" + d.Name);
        getDirectory(sw, d.FullName, indent + 2);
        sw.WriteLine();
    }
}

  

  

转载于:https://mp.weixin.qq.com/s/n1LtDr7uMxNJmtko4NiJJg

 

posted on   SkyLine。  阅读(1171)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示