c#文件读写操作

1.read Text file
try
{
FileStream fs = new FileStream(@"c:\test.txt", FileMode.Open, FileAccess.Read); 
StreamReader sr = new StreamReader(fs);  
  string   curLine;  
  while(   (curLine   =   sr.ReadLine())   !=   null   )
{

sr.Close();  
}
catch
{
}

2.Write Text File

try
{
        FileStream fs=new FileStream("c:\\Variables.txt", FileMode.Append, FileAccess.Write, FileShare.Write);
        fs.Close();
        StreamWriter sw=new StreamWriter("c:\\Variables.txt", true, Encoding.ASCII);
        string NextLine="This is the appended line.";
        sw.Write(NextLine);
        sw.Close();
}
catch
{
}


3.Read/write binary File

4.delete File
if (File.Exists(@"c:\test.txt"))
{
    File.Delete(@"c:\test.txt"); 
}


 

你可能感兴趣的:(文件读写)