简体   繁体   English

如何在目录中显示文件

[英]how to display files inside directory

i am trying to get list of files inside the directory in this case "c:\\dir\\" (ofcourse i have files inside) and i wanted to display the name of those files in console program build in c#.... 我试图获取目录中的文件列表,在这种情况下“c:\\ dir \\”(当然我有文件里面)我想在c#中显示控制台程序构建中的那些文件的名称....

initially i did this.... 最初我这样做....

static class Program
    {
        static void Main()
        {
            string[] filePaths = Directory.GetFiles(@"c:\dir\");
            Console.WriteLine();
            Console.Read();


        }
    }

how can i see the name of those files....... any help would be appreciated..... 我怎么能看到这些文件的名称.......任何帮助将不胜感激.....

thank you. 谢谢。

(further i would like to know if possible any idea on sending those file path towards dynamic html page.... any general concept how to do that...) (进一步我想知道是否有任何想法将这些文件路径发送到动态html页面....任何一般概念如何做到这一点......)

If by "file names" you mean literally just the names and not the full paths : 如果用“文件名”表示字面上只是名称不是完整路径

string[] filePaths = Directory.GetFiles(@"c:\dir");
for (int i = 0; i < filePaths.Length; ++i) {
    string path = filePaths[i];
    Console.WriteLine(System.IO.Path.GetFileName(path));
}

Loop through the files and print them one at a time: 循环遍历文件并一次打印一个:

foreach(string folder in Directory.GetDirectories(@"C:\dir"))
{
    Console.WriteLine(folder);
}

foreach(string file in Directory.GetFiles(@"C:\dir"))
{
    Console.WriteLine(file);
}
foreach (string filename in filePaths) {
  Console.WriteLine(filename);
}

Keep in mind that if there are many files in the folder you will have memory problems. 请记住,如果文件夹中有许多文件,则会出现内存问题。
.Net 4.0 contains a fix: C# directory.getfiles memory help .Net 4.0包含一个修复: C#directory.getfiles内存帮助

foreach(FileInfo f in Directory.GetFiles()) Console.Writeline(f.Name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM