C#读取文件夹中的文件操作浅析
C#读取文件夹中的文件操作是怎么样子的呢?那么本文就向你介绍这方面的内容,希望对你有所帮助。
C#读取文件夹的操作是如何进行的呢?首先让我们来看啊可能:读出一个文件夹中的所有文件(文件数从0个到N多不定).没有文件返回假.
都是ascii码文件.
读每个文件的前一部分至出现第一个 /s./s/r 为止.
读出来放后放至string[] filetsr中.
- strFiles=Directory.GetFiles(@"c:/Import");
- foreach(stringstrFileinstrFiles)
- {
- File.Move(strFile,strFile.Replace("Import","Rubbish_Files"));
- //上面这句换为你的C#读取文件夹处理。
- }
试试看C#读取文件夹的代码吧,没来得及调试,有问题自己改改吧!
- usingSystem;
- usingSystem.IO;
- classTest
- {
- publicstaticvoidMain()
- {
- try
- {
- //Onlygetfilesthatbeginwiththeletter"c."
- inti=0;
- string[]dirs=Directory.GetFiles(@"c:/","c*");
- Console.WriteLine("Thenumberoffilesstartingwithcis{0}.",dirs.Length);
- string[]filetsr=newString[dirs.Length];
- foreach(stringdirindirs)
- {
- FileStreamfs=newFileStream(dir,FileMode.Open);
- byte[]readBuf=newbyte[fs.Length];
- syncF.Read(readBuf,0,fs.Length);
- data=Encoding.ASCII.GetString(readBuf);
- if(data.IndexOf("/s./s/r")>-1)
- {
- i++;
- filestr[i]=Microsoft.Basic.Left(data.IndexOf("/s./s/r")-1)
- }
- }
- }
- catch(Exceptione)
- {
- Console.WriteLine("Theprocessfailed:{0}",e.ToString());
- }
- }
- }
C#中遍历文件夹目录的问题
C#读取文件夹之递归实现查找目录下的所有子目录和文件
- publicvoidFindFile(stringdir)//参数为指定的目录
- {
- //C#读取文件夹在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
- DirectoryInfoDir=newDirectoryInfo(dir);
- try
- {
- foreach(DirectoryInfodinDir.GetDirectories())//查找子目录
- {
- FindFile(Dir+d.ToString()+"//");
- listBox1.Items.Add(Dir+d.ToString()+"//");//listBox1中填加目录名
- }
- foreach(FileInfofinDir.GetFiles("*.*"))//查找文件
- {
- listBox1.Items.Add(Dir+f.ToString());//listBox1中填加文件名
- }
- }
- catch(Exceptione)
- {
- MessageBox.Show(e.Message);
- }
- }
C#读取文件夹之方法调用情况:
- privatevoidbutton1_Click(objectsender,System.EventArgse)
- {
- stringcurrentdir="F://myprogram//C#//FileSearch";//搜索的目录
- if(currentdir[currentdir.Length-1]!='//')//非根目录
- currentdir+="//";
- FindFile(currentdir);//调用查找文件函数
- }
C#读取文件夹中的文件的基本的情况就向你介绍到这里,希望对你学习C#读取文件夹有所帮助。