如何使用C#列出目录中的所有文件 [英] How to list out all the files in a directory using C#

查看:96
本文介绍了如何使用C#列出目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有文件移动到应该在运行时创建的目录中



例如。



C:\ usersrs \Desktop \Sample





它有一些文件,如



file1

file2

file3

file4





这里我需要做的是,我想创建一个所有文件都是商店的目录

那就是C:\ usersrs \Desktop \\ \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n $ \\ n \\ n




我想创建一个名为Temp的目录。



现在我想将所有文件移动到Temp目录。



任何人都可以给我代码

解决方案

尝试这个简单的代码: -

 string [] fileArray = Directory.GetFiles(@c:\Dir \); 





  class 计划
{
st atic void Main()
{
// 将根目录中的所有文件名放入数组中。
string [] array1 = Directory.GetFiles(< span class =code-string> @ C:\);


// 显示所有文件。
Console.WriteLine( --- Files:---);
foreach 字符串名称 in array1)
{
Console.WriteLine(name);
}

}





参考: -

http://www.dotnetperls.com/directory-getfiles [ ^ ]

http://msdn.microsoft.com/en-us/library/07wt70x2(v = vs.110).aspx [ ^ ]


包括

 使用系统; 
使用 System.IO;





string [] files = Directory.GetFiles(@C:\);

  foreach  string  name  in  files)
{
Console.WriteLine(name);
}


  string  [] array1 = Directory.GetFiles( @  C:\); 
foreach 字符串名称 in array1)
{
Console.WriteLine(name);
}





问候,



Praveen


I want to move all files into a directory which should be created at runtime

For Example.

C:\users\Desktop\Sample


Its has some files such as

file1
file2
file3
file4


Here what I need to do is, I want to create a directory where all files are stores
That is C:\users\Desktop\Sample


I want to create a directory named Temp.

Now I want to move all the files into the Temp directory.

Can anyone get me the code

解决方案

try this simple code:-

string[] fileArray = Directory.GetFiles(@"c:\Dir\");


or

class Program
{
    static void Main()
    {
    // Put all file names in root directory into array.
    string[] array1 = Directory.GetFiles(@"C:\");

   
    // Display all files.
    Console.WriteLine("--- Files: ---");
    foreach (string name in array1)
    {
        Console.WriteLine(name);
    }  
   
}



refer:-
http://www.dotnetperls.com/directory-getfiles[^]
http://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx[^]


include

using System;
using System.IO;



string[] files = Directory.GetFiles(@"C:\");

foreach (string name in files)
    {
        Console.WriteLine(name);
    }


string[] array1 = Directory.GetFiles(@"C:\");
foreach (string name in array1)
    {
        Console.WriteLine(name);
    }



Regards,

Praveen


这篇关于如何使用C#列出目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆