c# 中操作文本

来源:互联网 发布:java编程题库 编辑:程序博客网 时间:2024/05/01 08:56

 

创建文本:
string path;
path=@Application.StartupPath+"//log//log"+DateTime.Now.ToString("yyyy-MM--dd hhmmss")+".txt";
StreamWriter sw=File.CreateText(path);
sw.Write("hello");  
sw.WriteLine("hello");  
sw.Close();
­
读取文本
string   str;  
string   address="";  
FileStream   fs;  
string path;
path=Application.StartupPath+"//log//"+"1.txt";
try  
{  
fs=new   FileStream(path,FileMode.OpenOrCreate);  
}  
catch(FileNotFoundException   ex)  
{  
   MessageBox.Show("读取文件出现错误!"+ex.Message,"错误",0x00);  
   return;  
}  
StreamReader   sr=new   StreamReader(fs);  
    
while((str=sr.ReadLine())!=null)  
{  
address=address+str;  
}  
    
sr.Close();  
原创粉丝点击