【问题标题】:to get the file names in a directory (without getting path) in c#在 C# 中获取目录中的文件名(不获取路径)
【发布时间】:2014-03-20 06:49:01
【问题描述】:

我想将c:\test\中存在的所有文件的名称存储在字符串array s[]

让 c:\test 中有文件名a1.txt , a2.txt , a3.txt ...

我想存储在s[0]= a1.txt s[1]= a2.txt 之类的

我已经使用了代码

s = Directory.GetFiles(@"c:\\test");

但它使s[0]= c:\test\a1.txt 我不要c:\test,我只想要a1.txt

那么有什么方法可以只存储文件名而不存储文件的路径

我也想知道是否有任何方法可以从字符串数组的每个字符串中删除一些字符

就像从字符串数组的每个字符串的开头切割 5 个字符,这也可以解决我的问题。

【问题讨论】:

标签: c# filenames arrays


【解决方案1】:

使用GetFileName 从路径中提取文件名。如下所示

string[] s = Directory.GetFiles(@"c:\\test");
foreach (string filename in s)
{     
    string fname = Path.GetFileName(filename);
}

【讨论】:

  • @c_sharp boy 我很高兴你能找到解决方案,如果有帮助请接受这个答案。
  • for (x = 0; x
【解决方案2】:

可能来自hare.. @Vasea 给我们的任何方式:

Directory.GetFiles(@"c:\test", "*", SearchOption.TopDirectoryOnly).Select(f => Path.GetFileName(f));

【讨论】:

    猜你喜欢
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多