请教 我想从文本文件中输出一段文字,请问如何解决?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Use a TextReader to read the source file, and use a TextWriter to write to the destination file. //读/写文本文件示例代码:读:FileInfo fi = new FileInfo (_filepath);FileStream fs;//将文件读取到文件流中fs = fi.OpenRead ();//声明一读取文件流类的实例,用以读取文件流StreamReader sr = new StreamReader (fs);string myreader = sr.ReadLine() ;//文件数据while(myreader != null){ ...//做处理myreader = sr.ReadLine();}写: private void WriteLog(string msg) { StreamWriter sw = null; try { if(_logName == null || _logName == "") { _logName = this._filepath.Substring(0,_filepath.LastIndexOf(@"\")+1)+"PortLog.txt" ; } if(Directory.Exists(_logName.Substring(0,_logName.LastIndexOf(@"\")))) { if(File.Exists(_logName)) sw = File.AppendText(_logName ) ; else sw = File.CreateText(_logName ) ; sw.WriteLine(System.DateTime.Now.ToString()+"-"+msg) ; sw.Flush() ; } } catch(Exception) { throw new Exception("Error occured when writing log.") ; } finally { sw.Close() ; } } 那不就是先读出来再写到另一个文件里吗,Easy楼上正解 c#怎么刷qq空间留言!~~怎么刷淘宝流量 有人用过 DevExpress没 C#模拟登录 能帮我看看吗? 想不出了 用C# 但是不要用指针 抽象类实现接口的意义是什么? 两个字符串的重新组合,谢谢! 为什么ItemTemplate里的按纽点击的时候会报错 求助,开发一个连接智能手机发送短的软件需要怎么做? 大家一定要帮我啊! C#怎样不用自已的与别人写的assembly只用net平台自带的库写播放CD程序??? 如何在access中存储图片 C#开发的局域网聊天软件--布谷鸟2007
读:
FileInfo fi = new FileInfo (_filepath);
FileStream fs;
//将文件读取到文件流中
fs = fi.OpenRead ();
//声明一读取文件流类的实例,用以读取文件流
StreamReader sr = new StreamReader (fs);
string myreader = sr.ReadLine() ;//文件数据
while(myreader != null)
{
...//做处理
myreader = sr.ReadLine();
}
写:
private void WriteLog(string msg)
{
StreamWriter sw = null;
try
{
if(_logName == null || _logName == "")
{
_logName = this._filepath.Substring(0,_filepath.LastIndexOf(@"\")+1)+"PortLog.txt" ;
}
if(Directory.Exists(_logName.Substring(0,_logName.LastIndexOf(@"\"))))
{
if(File.Exists(_logName))
sw = File.AppendText(_logName ) ;
else
sw = File.CreateText(_logName ) ;
sw.WriteLine(System.DateTime.Now.ToString()+"-"+msg) ;
sw.Flush() ;
}
}
catch(Exception)
{
throw new Exception("Error occured when writing log.") ;
}
finally
{
sw.Close() ;
}
}
楼上正解