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"); 
}


 

posted on 2007-12-24 14:11  highmayor  阅读(650)  评论(0编辑  收藏  举报

导航