操作文件 (File和FileInfo类)

使用File和FileInfo类操作文件 

.NET Framework类库包含用于处理文件的两个类似的类:FileInfo和File.

File类为创建、删除和操作文件提供静态方法,而FileInfo类为文件操作提供实例方法。类似与Directory类,File类只提供静态方法,而没有包含任何特性。

 

view plain copy to clipboard print ?
  1. using System;  
  2. using System.IO;  
  3.   
  4. namespace ConsoleApplicationFile  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             string filePath = @"D:/temp/textfile.txt";  
  11.             string fileCopyPath = @"D:/temp/textfile_copy.txt";  
  12.             string newFileName = @"D:/temp/textfile_newcopy.txt";  
  13.   
  14.             try  
  15.             {  
  16.                 //---if file already existed---  
  17.                 if (File.Exists(filePath))  
  18.                 {  
  19.                     //---delete the file---  
  20.                     File.Delete(filePath);  
  21.                 }  
  22.   
  23.                 //---create the file again  
  24.                 FileStream fs = File.Create(filePath);  
  25.                 fs.Close();  
  26.   
  27.                 //---make a copy of the file---  
  28.                 File.Copy(filePath, fileCopyPath);  
  29.   
  30.                 //---rename the file---  
  31.                 File.Move(fileCopyPath, newFileName);  
  32.   
  33.                 //---display the creation time---  
  34.                 Console.WriteLine(File.GetCreationTime(newFileName));  
  35.   
  36.                 //---make the file read-only and hidden---  
  37.                 File.SetAttributes(newFileName, FileAttributes.ReadOnly);  
  38.                 File.SetAttributes(newFileName, FileAttributes.Hidden);  
  39.             }  
  40.             catch (IOException ex)  
  41.             {  
  42.                 Console.WriteLine(ex.Message);  
  43.             }  
  44.             catch (Exception ex)  
  45.             {  
  46.                 Console.WriteLine(ex.Message);  
  47.             }  
  48.   
  49.             Console.ReadLine();  
  50.         }  
  51.     }  
  52. }  

 

File类包含用于将内容写入文件的4种方法:

  • WriteAllText():创建文本,将字符串写入该文件,关闭文件
  • AppendAllText():将字符串附加到已有的文件
  • WriteAllLines():创建文件,将字符串数组写入该文件,关闭文件
  • WriteAllBytes():创建文件,将字节数组写入该文件,关闭文件

下面的代码显示了如何使用各种方法将一些内容写入文件:

 

view plain copy to clipboard print ?
  1. using System;  
  2. using System.IO;  
  3. using System.Text;  
  4.   
  5. namespace ConsoleApplicationFile  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             string filePath = @"D:/temp/textfile.txt";  
  12.             string strTextToWrite = "This is a string";  
  13.             string[] strLinesToWrite = new string[] { "Line1","Line2"};  
  14.             byte[] bytesToWrite = ASCIIEncoding.ASCII.GetBytes("This is a string");  
  15.   
  16.            
  17.             try  
  18.             {  
  19.                 File.WriteAllText(filePath, strTextToWrite);  
  20.                 File.AppendAllText(filePath, strTextToWrite);  
  21.                 File.WriteAllLines(filePath, strLinesToWrite);  
  22.                 File.WriteAllBytes(filePath, bytesToWrite);  
  23.             }  
  24.             catch (IOException ex)  
  25.             {  
  26.                 Console.WriteLine(ex.Message);  
  27.             }  
  28.             catch (Exception ex)  
  29.             {  
  30.                 Console.WriteLine(ex.Message);  
  31.             }  
  32.   
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  

 

File类也包含用于从文件读取内容的3种方法:

  • ReadAllText():打开文件,读取该文件中的所有文本并放入字符串中,关闭文件
  • ReadAllLines():打开文件,读取该文件中的所有文本并放入字符串数组中,关闭文件
  • ReadAllBytes():打开文件,读取该文件中的所有文本并放入字节数组中,关闭文件

你可能感兴趣的:(c#)