zoukankan      html  css  js  c++  java
  • 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);//删除子目录和文件
                            }
  • 相关阅读:
    CSU 1333 Funny Car Racing
    FZU 2195 检查站点
    FZU 2193 So Hard
    ZOJ 1655 FZU 1125 Transport Goods
    zoj 2750 Idiomatic Phrases Game
    hdu 1874 畅通工程续
    hdu 2489 Minimal Ratio Tree
    hdu 3398 String
    洛谷 P2158 [SDOI2008]仪仗队 解题报告
    POJ 1958 Strange Towers of Hanoi 解题报告
  • 原文地址:https://www.cnblogs.com/lijianda/p/7074718.html
Copyright © 2011-2022 走看看