日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

C#创建文件夹和文件

發布時間:2023/12/15 综合教程 32 生活家
生活随笔 收集整理的這篇文章主要介紹了 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);//刪除子目錄和文件
                        }

總結

以上是生活随笔為你收集整理的C#创建文件夹和文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。