文件操作概览

mac2022-06-30  62

趁有空,对.net的文件操作类做了一个大概的总结.

首先引用IO namespace:

using  System.IO;

创建目录:

         ///   <summary>          ///  Directory.CreateDirectory参数解释:          ///  path          ///  要创建的目录。          ///  directorySecurity          ///  要应用于此目录的访问控制。          ///            ///  如果文件夹存在则不创建,可用Directory.Exists判断文件夹是否存在.          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button1_Click( object  sender, EventArgs e)         {              try             {                 DirectoryInfo di  =  Directory.CreateDirectory( @" C:\FileOperator " );                  string  message  =   string .Format( " Create \ " { 0 }\ "  success! " , di.FullName);                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Create folder Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

创建文件:

         ///   <summary>          ///  File.Create参数解释:          ///  path          ///  类型:System..::.String          ///  文件名。          ///  bufferSize          ///  类型:System..::.Int32          ///  用于读取和写入文件的已放入缓冲区的字节数。          ///  options          ///  类型:System.IO..::.FileOptions          ///  FileOptions 值之一,它描述如何创建或覆盖该文件。          ///  fileSecurity          ///  类型:System.Security.AccessControl..::.FileSecurity          ///  FileSecurity 值之一,它确定文件的访问控制和审核安全性。          ///  创建文件会覆盖原来同名文件,所以创建前最好File.Exists判断文件是否存在.          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button2_Click( object  sender, EventArgs e)         {              try             {                 FileStream fs  =  File.Create( @" C:\FileOperator\File1.txt " );                  string  message  =   string .Format( " Create \ " { 0 }\ "  success! " , fs.Name);                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);                 fs.Dispose();                 fs.Close();             }              catch             {                 MessageBox.Show( " Create file Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

打开和写入文件:

         ///   <summary>          ///  File.Open参数解释:          ///  path          ///  类型:System..::.String          ///  要打开的文件。          ///  mode          ///  类型:System.IO..::.FileMode          ///  FileMode 值,用于指定在文件不存在时是否创建该文件,并确定是保留还是覆盖现有文件的内容。          ///  access          ///  类型:System.IO..::.FileAccess          ///  FileAccess 值,指定可以对文件执行的操作。          ///  share          ///  类型:System.IO..::.FileShare          ///  FileShare 值,它指定其他线程所具有的对该文件的访问类型。          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button3_Click( object  sender, EventArgs e)         {              try             {                 FileStream fs  =  File.Open( @" C:\FileOperator\File1.txt " , FileMode.Append);                  byte [] byteArray  =   new   byte [] { ( byte ) ' K ' , ( byte ) ' E ' , ( byte ) ' N '  };                 fs.Write(byteArray,  0 , byteArray.Length);                  string  message  =   string .Format( " Write \ " KEN\ "  to File success! " , fs.Name);                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);                 fs.Dispose();                 fs.Close();             }              catch             {                 MessageBox.Show( " Open & Write file Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

文件复制:

         ///   <summary>          ///  File.Copy参数解释:          ///  sourceFileName: 源文件          ///  destFileName: 目标文件          ///  overwrite: 如果目标已存在,是否覆盖目标文件          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button4_Click( object  sender, EventArgs e)         {              try             {                 DirectoryInfo di  =  Directory.CreateDirectory( @" C:\FileOperator\Temp " );                 File.Copy( @" C:\FileOperator\File1.txt " @" C:\FileOperator\Temp\File1.txt " true );                  string  message  =   string .Format( " Copy File to \ " { 0 }\ "  success! " @" C:\FileOperator\Temp\File1.txt " );                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Copy file Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

文件删除:

         ///   <summary>          ///  File.Delete参数解释:          ///  path: 文件完整路径;          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button5_Click( object  sender, EventArgs e)         {              try             {                 File.Delete( @" C:\FileOperator\Temp\File1.txt " );                  string  message  =   " Delete File success! " ;                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Delete file Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

移动和重命名:

         ///   <summary>          ///  File.Move参数解释:          ///  sourceFileName: 源文件          ///  destFileName: 目标文件          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button6_Click( object  sender, EventArgs e)         {              try             {                  // move                 File.Move( @" C:\FileOperator\File1.txt " @" C:\FileOperator\Temp\File1.txt " );                  // rename                 File.Move( @" C:\FileOperator\Temp\File1.txt " @" C:\FileOperator\Temp\File2.txt " );                  string  message  =   " Move & Rename file success! " ;                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Move & Rename file Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }              // 文件夹的Move和Rename同文件一样,也使用File.Move,传入文件路径即可.         }

设置文件属性:

         ///   <summary>          ///  File.SetAttributes参数解释:          ///  path: 文件完整路径;          ///  fileAttributes: FileAttributes值,指定文件属性.          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button7_Click( object  sender, EventArgs e)         {              try             {                 File.SetAttributes( @" C:\FileOperator\Temp\File2.txt " , FileAttributes.ReadOnly  |  FileAttributes.Hidden);                  string  message  =   " Set file Readonly & Hidden success! " ;                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Set file attributes Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

检查文件和目录是否存在:

         ///   <summary>          ///  参数解释:          ///  path:文件路径          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button8_Click( object  sender, EventArgs e)         {              try             {                  if  (Directory.Exists( @" C:\FileOperator\Temp " ))                     MessageBox.Show( string .Format( " \ " { 0 }\ " Exits! " @" C:\FileOperator\Temp\ " ),  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);                  if  (File.Exists( @" C:\FileOperator\Temp\File2.txt " ))                     MessageBox.Show( string .Format( " \ " { 0 }\ " Exits! " @" C:\FileOperator\Temp\File2.txt " ),  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Check Folder & File Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

设置目录属性:

         ///   <summary>          ///  不用解释          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button9_Click( object  sender, EventArgs e)         {              try             {                 DirectoryInfo di  =   new  DirectoryInfo( @" C:\FileOperator\Temp " );                 di.Attributes  =  FileAttributes.ReadOnly  |  FileAttributes.Hidden;                  string  message  =   " Set folder attributes success! " ;                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Set folder attributes Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

获取子目录:  

         ///   <summary>          ///  懒得解释          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button10_Click( object  sender, EventArgs e)         {              try             {                  string [] children  =  Directory.GetDirectories( @" C:\FileOperator " );                  string  message  =   string .Empty;                  if  (children.Length  >   0 )                     message  =   string .Format( " {0} folders in \ " { 1 }\ "" , children.Length,  @" C:\FileOperator " );                  else                     message  =   string .Format( " No folder in \ " { 0 }\ "" @" C:\FileOperator " );                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Get child folder Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

获取目录下所有文件:

         ///   <summary>          ///  呵呵          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button11_Click( object  sender, EventArgs e)         {              try             {                  string [] files  =  Directory.GetFiles( @" C:\FileOperator " );                  string  message  =   string .Empty;                  if  (files.Length  >   0 )                     message  =   string .Format( " {0} files in \ " { 1 }\ "" , files.Length,  @" C:\FileOperator " );                  else                     message  =   string .Format( " No file in \ " { 0 }\ "" @" C:\FileOperator " );                 MessageBox.Show(message,  " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Get files Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

删除目录:

         ///   <summary>          ///  参数解释:          ///  path:          ///  recursive:是否递归删除目录下所有目录及文件.          ///   </summary>          ///   <param name="sender"></param>          ///   <param name="e"></param>          private   void  button12_Click( object  sender, EventArgs e)         {              try             {                  // 正常文件才可以删除                 DirectoryInfo di  =   new  DirectoryInfo( @" C:\FileOperator\Temp " );                 di.Attributes  =  FileAttributes.Normal;                 File.SetAttributes( @" C:\FileOperator\Temp\File2.txt " , FileAttributes.Normal);                  // delete                 Directory.Delete( @" C:\FileOperator " true );                 MessageBox.Show( " Delete folder success! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Information);             }              catch             {                 MessageBox.Show( " Delete folder Error! " " 系统消息 " , MessageBoxButtons.OK, MessageBoxIcon.Error);             }         }

 

另外,.Net对txt文件提供了更友好的操作封装,具体请查看MSDN,path参数可以用绝对路径,也可以使用相对路径,个人推荐使用绝对路径.

全部操作源代码,请点击 源代码 下载.

转载于:https://www.cnblogs.com/KenBlove/archive/2008/09/27/1300426.html

最新回复(0)