C#中IO操作

using sysytem.Io; 

File.Exists()  检查文件是否存在,

Directory.Exists()  检查文件夹是否存在

FileInfo DirectoryInfo 可实例化  对文件的具体操作

1 FileStream  fs  = new FileStream('文件名',FileMode.Create);          //创建文件流
2 
3 BinaryWriter  w  = new BinaryWriter(fs);                //打开写入器
4 
5     w.Write("写入的内容");
6 
7     w.Close();             //关闭写入器
8 
9     fs.Close();             // 关闭文件流
写入操作
 1 using(StreamWriter w  = File.AppendText("文件名"))
 2 {
 3        Log("追加的内容", w);
 4        w.Close();  
 5     
 6 }
 7 
 8 public static  void  Log (string  message,TextWriter  w)
 9 {
10       w.WriteLine(message);
11      w.Flush();  
12 }
追加写入
 1 FileStream  fs  = new FileStream('文件名',FileMode.Open, FileAccess.Read);         //创建文件流
 2 
 3 BinaryReader  w  = new BinaryReader(fs);                //打开阅读器
 4 
 5     w.ReadString();
 6 
 7     w.Close();             
 8 
 9     fs.Close();             // 关闭文件流
10 
11 写入操作
读取文件

 

猜你喜欢

转载自www.cnblogs.com/hegezhishouzhetian/p/9219508.html