创建文本:
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();
本文介绍了使用C#进行文件的创建与读取操作的方法。包括如何创建文本文件并写入内容,以及如何从指定路径读取文件内容到字符串变量中。
2076

被折叠的 条评论
为什么被折叠?



