C#创建文件夹和文件

一、创建文件夹,例:

1  if (!Directory.Exists(path))
2                 {
3                     Directory.CreateDirectory(path);
4                 }

 

二、创建文件,例:

 1  global::System.IO.FileInfo josnfile = new global::System.IO.FileInfo(JsonPath);
 2                     if (!josnfile.Exists)
 3                     {
 4                         // 创建map.json文件
 5                         FileStream fs = new FileStream(JsonPath, FileMode.CreateNew, FileAccess.ReadWrite);
 6                         StreamWriter sw = new StreamWriter(fs);
 7                         sw.Write("[]");
 8                         sw.Flush();
 9                         sw.Close();
10                         //Thread.Sleep(300);
11                     }

 三、遍历文件夹下的所有文件或文件夹

遍历文件:

//录像文件
string videoPath = fileManager.TrimEnd('\\') + "\\" + item.CourtID + "\\Conference\\" + item.ID;
if(Directory.Exists(videoPath))
{   DirectoryInfo TheFolder
= new DirectoryInfo(videoPath);   //遍历文件   foreach (global::System.IO.FileInfo NextFile in TheFolder.GetFiles()) { } }

遍历文件夹:

if(Directory.Exists(videoPath))
{
  DirectoryInfo TheFolder=new DirectoryInfo(videoPath);
  //遍历文件夹
  foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
  {
  }
}

 

四、读取文件内容,例:

1                 using (FileStream fs = new FileStream(JsonPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
2                 {
3                     using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
4                     {
5                         noteIsSubmit = sr.ReadToEnd().ToString().Contains(FileName);
6                     }
7                 }

 

五、复制文件,例:

 1  global::System.IO.FileInfo _f = new global::System.IO.FileInfo(path);
 2                 try
 3                 {
 4                     if (!_f.Exists)
 5                     {
 6                         //复制讲稿文件
 7                         global::System.IO.FileInfo copyFile = new global::System.IO.FileInfo(FileURL);
 8                         copyFile.CopyTo(path);
 9                     }
11                 }
12                 catch (Exception ex)
13                 {
14                     Logger.D("NoteMake讲稿制作发生异常:", ex.Message);
15                 }

 六、删除指定文件,例:

string path = FileManager.BASEPATH + "\\" + item.CourtID + "\\Topics\\" + item.ID + "\\" + item.Type + ".doc";
                global::System.IO.FileInfo _f = new global::System.IO.FileInfo(path);
                if (_f.Exists)
                {
                    global::System.IO.File.Delete(path);
                }

 七、删除指定文件夹,例:

//讲稿标注文档路径
                        string noteFilePath = item.FilePath.Substring(0, item.FilePath.LastIndexOf('.'));
                        if (Directory.Exists(noteFilePath))
                        {
                            DirectoryInfo _d = new DirectoryInfo(noteFilePath);
                            _d.Delete(true);//删除子目录和文件
                        }

 

转载于:https://www.cnblogs.com/lijianda/p/7074718.html

  • 1
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值