开发者

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#....

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开发者_如何学Go path towards dynamic html page.... any general concept how to do that...)


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜