有关对文件的操作(System.IO)

来源:互联网 发布:java编辑器哪个好 编辑:程序博客网 时间:2024/04/30 08:53

操作文件常用的类有:
File------实用类,提供许多静态方法,用于移动、删除、和复制文件。
Directory------实用类,提供许多静态方法,用于移动、删除和复制目录。
Path------ 实用类,用于处理路徑名称。
FileInfo------表示磁盘上的物理文件,具有可以处理此文件的方法,要完成对文件的读写工作,就必须创建Stream对像。
DirectoryInfo------表示磁盘上的物理目录,具有可以处理此目录的方法

 

实例:

  1.   public class FileHandle
  2.     {
  3.         public FileHandle()
  4.         { }
  5.         #region 检查文件夹是否存在,如果不存在,则创建新的
  6.         /// <summary>
  7.         /// 检查文件夹是否存在,如果不存在,则创建新的
  8.         /// </summary>
  9.         /// <param name="dir">父级目录地址</param>
  10.         /// <param name="path">子级目录地址</param>
  11.         public void CheckPath(string dir, string path)
  12.         {
  13.             #region 检查文件夹是否存在,如果不存在,则创建新的
  14.             string[] dirs = Directory.GetDirectories(dir);//得到本目录下的所有文件夹路径
  15.             bool tag = false;
  16.             try
  17.             {
  18.                 foreach (string dirName in dirs)
  19.                 {
  20.                     if (dirName.ToLower() == path.ToLower())//如果传入的路径存在于文件夹中
  21.                     {
  22.                         tag = true;
  23.                     }
  24.                 }
  25.                 if (!tag)
  26.                 {
  27.                     System.IO.Directory.CreateDirectory(path);//创建文件夹
  28.                 }
  29.             }
  30.             catch
  31.             { }
  32.             #endregion
  33.         }
  34.         #endregion
  35.         #region 文件的迁移
  36.         /// <summary>
  37.         /// 文件的迁移
  38.         /// </summary>
  39.         /// <param name="oldUrl">原文件地址</param>
  40.         /// <param name="newUrl">目标地址</param>
  41.         public void CopeFile(string oldUrl, string newUrl)
  42.         {
  43.             #region
  44.             if (!File.Exists(newUrl))//判断文件是存在.
  45.             {
  46.                 File.Copy(oldUrl, newUrl, false);//将文件迁移,并是否覆盖
  47.             }
  48.             else
  49.             {
  50.                 File.Copy(oldUrl, newUrl, true);//将文件迁移,存在则覆盖
  51.             }
  52.             #endregion
  53.         }
  54.         #endregion
  55.         #region 文件夹的迁移
  56.         /// <summary>
  57.         /// 文件夹的迁移
  58.         /// </summary>
  59.         /// <param name="oldDir">老目录</param>
  60.         /// <param name="newDir">新目录</param>
  61.         /// <param name="flag"></param>
  62.         public void CopFile(string oldDir, string newDir, bool flag)
  63.         {
  64.             #region 文件夹的迁移
  65.             DirectoryInfo m_dir = new DirectoryInfo(oldDir);//创建一个相同名字的目录
  66.             foreach (FileSystemInfo m_fsi in m_dir.GetFileSystemInfos())//目录中所有文件和子目录的类型.
  67.             {
  68.                 string m_newDir = Path.Combine(newDir, m_fsi);
  69.                 if (m_fsi is DirectoryInfo)
  70.                 {
  71.                     Directory.CreateDirectory(m_newDir);//如果为目录刚创建
  72.                 }
  73.                 else
  74.                 {
  75.                     File.Copy(m_fsi.FullName, m_newDir);//文件直接复制
  76.                 }
  77.             }
  78.             #endregion
  79.         }
  80.         #endregion
  81.         #region C#追加文件
  82.         public void AddContentToFile(string filePath)
  83.         {
  84.             #region
  85.             StreamWriter sw=File.AppendAllText(filePath);//将指定的字符追加到文件中,如果不存在,则创建文件
  86.             sw.WriteLine("陈琳");
  87.             sw.WriteLine("的幸福生活");
  88.             sw.WriteLine("从现在开始哦");
  89.             sw.Flush();
  90.             sw.Close();
  91.             #endregion
  92.         }
  93.         #endregion
  94.         #region 删除文件
  95.         public void DelFile(string filepath)
  96.         {
  97.             File.Delete(filepath);//删除文件,不存在,不会发出异常
  98.         }
  99.         #endregion
  100.         #region 移动文件
  101.         public void MoveFile(string oldPath, string newPath)
  102.         {
  103.             File.Move(oldPath, newPath);
  104.         }
  105.         #endregion
  106.         #region 创建目录
  107.         public void CreateFolder()
  108.         {
  109.             #region 创建目录
  110.             // 创建目录c:/sixAge 
  111.             DirectoryInfo d = Directory.CreateDirectory("c://sixAge");
  112.             // d1指向c:/sixAge/sixAge1 
  113.             DirectoryInfo d1 = d.CreateSubdirectory("sixAge1");
  114.             // d2指向c:/sixAge/sixAge1/sixAge1_1 
  115.             DirectoryInfo d2 = d1.CreateSubdirectory("sixAge1_1");
  116.             // 将当前目录设为c:/sixAge 
  117.             Directory.SetCurrentDirectory("c://sixAge");
  118.             // 创建目录c:/sixAge/sixAge2 
  119.             Directory.CreateDirectory("sixAge2");
  120.             // 创建目录c:/sixAge/sixAge2/sixAge2_1 
  121.             Directory.CreateDirectory("sixAge2//sixAge2_1");
  122.             #endregion
  123.         }
  124.         #endregion
  125.         #region 递归删除文件及文件夹
  126.         public void DeleteFolder(string dir)
  127.         {
  128.             #region
  129.             if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
  130.             {
  131.                 foreach (string d in Directory.GetFileSystemEntries(dir))//文件和字目录的名称
  132.                 {
  133.                     if (File.Exists(d))
  134.                     {
  135.                         File.Delete(d); //直接删除其中的文件 
  136.                     }
  137.                     else
  138.                     {
  139.                         DeleteFolder(d); //递归删除子文件夹 
  140.                     }
  141.                 }
  142.                 Directory.Delete(dir); //删除已空文件夹 
  143.                 Response.Write(dir + " 文件夹删除成功");
  144.             }
  145.             else
  146.             {
  147.                 Response.Write(dir + " 该文件夹不存在"); //如果文件夹不存在则提示
  148.             }
  149.             #endregion
  150.         }
  151.         #endregion
  152.         #region 读取文本文件        
  153.         public void ReadFromTxtFile(string txtFileName)
  154.         {
  155.             StreamReader fileStream = new StreamReader(txtFileName, Encoding.Default);
  156.             Console.WriteLine(fileStream.ReadToEnd());//从头读到结束.
  157.             fileStream.Close();
  158.         }
  159.         #endregion
  160.     }
原创粉丝点击