C#中怎么读取文件夹中的文件

发布时间:2021-07-07 16:20:20 作者:Leah
来源:亿速云 阅读:321

本篇文章为大家展示了C#中怎么读取文件夹中的文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

C#读取文件夹的操作是如何进行的呢?首先让我们来看啊可能:读出一个文件夹中的所有文件(文件数从0个到N多不定).没有文件返回假.

都是ascii码文件.

读每个文件的前一部分至出现***个 /s./s/r 为止.

读出来放后放至string[] filetsr中.

strFiles = Directory.GetFiles(@"c:\Import");   foreach(string strFile in strFiles)   {   File.Move(strFile,strFile.Replace("Import","Rubbish_Files"));    //上面这句换为你的C#读取文件夹处理。    }

试试看C#读取文件夹的代码吧,没来得及调试,有问题自己改改吧!

using System;   using System.IO;    class Test   {   public static void Main()   {   try   {   // Only get files that begin with the letter "c."   int i=0;   string[] dirs = Directory.GetFiles(@"c:\", "c*");   Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);   string[] filetsr=new String[dirs.Length];   foreach (string dir in dirs)   {   FileStream fs = new FileStream(dir,FileMode.Open);   byte[] readBuf=new byte[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 (Exception e)   {   Console.WriteLine("The process failed: {0}", e.ToString());   }   }   }

C#中遍历文件夹目录的问题

C#读取文件夹之递归实现查找目录下的所有子目录和文件

public   void   FindFile(string   dir)   //参数为指定的目录  {       //C#读取文件夹在指定目录及子目录下查找文件,在listBox1中列出子目录及文件  DirectoryInfo   Dir=new   DirectoryInfo(dir);  try {        foreach(DirectoryInfo   d   in   Dir.GetDirectories())//查找子目录     {  FindFile(Dir+d.ToString()+"\\");  listBox1.Items.Add(Dir+d.ToString()+"\\");  //listBox1中填加目录名  }        foreach(FileInfo   f   in   Dir.GetFiles("*.*")) //查找文件  {  listBox1.Items.Add(Dir+f.ToString());  //listBox1中填加文件名  }  }  catch(Exception   e)  {  MessageBox.Show(e.Message);  }   }

C#读取文件夹之方法调用情况:

private   void   button1_Click(object   sender,   System.EventArgs   e)  {  string   currentdir="F:\\myprogram\\C#\\FileSearch";     //搜索的目录  if(currentdir[currentdir.Length-1]!='\\')   //非根目录  currentdir+="\\";     FindFile(currentdir);     //调用查找文件函数  }

上述内容就是C#中怎么读取文件夹中的文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. python如何读取多层嵌套文件夹中的文件
  2. 利用php怎么读取子文件夹下的文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:C#中如何使用String对象

下一篇:C#中怎么转换变量类型

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》