[问题] streamwriter reader在不同环境下不work

楼主: b26168 (b26168)   2016-04-12 11:39:57
使用
streamwriter reader
读写是不同的档案
在自己的电脑使用正常
在某部份电脑却没有work
似乎没读到内容
写档有create出file 但0kb
static class Constants
{
public const string LogFileDirectory = @"C:\ABC";
public const string LogFileName = "ChangeLog.dat";
public const string LogBakFileName = "ChangeLog.bak";
public const string DebugLogFileName = "ErrorLog.dat";
}
public MainWindow()
{
InitializeComponent();
string str = "";
if
(System.IO.File.Exists(System.IO.Path.Combine(Constants.LogFileDirectory,
Constants.DebugLogFileName)))
{
StreamReader sr = new
StreamReader(System.IO.Path.Combine(Constants.LogFileDirectory,
Constants.DebugLogFileName));
str = sr.ReadToEnd();
sr.Close();
}
sw = new
StreamWriter(System.IO.Path.Combine(Constants.LogFileDirectory,
Constants.DebugLogFileName));
sw.WriteLine(str + " ====== work start " + GetNowDateString() + "
====== ");
sw.WriteLine(" ====== work end " + GetNowDateString() + " ====== ");
sw.Close();
不知道该从何下手..
作者: TameFoxx (foxx)   2016-04-12 12:08:00
所以我说那个code勒至少把你streamwriter做事部分的codePO出来吧这个code看起来 就算没读到档应该还是会有写入东西可是为什么你读写的是同一个档案...?
楼主: b26168 (b26168)   2016-04-12 16:51:00
这个档案要接下去写下去 主要的不是这个档拉 这是debug用自己电脑试这段code是没问题的 想问是否环境有关?
作者: YaMeiLo (亚妹露~!!)   2016-04-12 18:43:00
Close 之前,先Flush可能你读的东西太少,还在buffer
作者: TameFoxx (foxx)   2016-04-12 18:54:00
不用 close会自动flush
楼主: b26168 (b26168)   2016-04-12 23:25:00
问题是我跟对方电脑读的档案内容一样 写出一样 对方却不行
作者: TameFoxx (foxx)   2016-04-13 09:16:00
不是阿 我怎么看你读档写档的路径都一样阿如果最后出来是0kb 代表你读的档案本身就是没东西因为是同一个阿..........
作者: Litfal (Litfal)   2016-04-13 12:24:00
如果你读出str没有要做其他事,根本不需要读出str,请用https://msdn.microsoft.com/library/36b035cb.aspx
作者: TameFoxx (foxx)   2016-04-13 12:27:00
照理说也不应该空白 只能试试看在close之前先sw.flush()
作者: Litfal (Litfal)   2016-04-13 12:33:00
这的确是匪夷所思,能建应该就能写,除非他写之前就跳错了例如str是null或GetNowDateString()出错
作者: TameFoxx (foxx)   2016-04-13 13:42:00
摁 那建议原po先单纯测试sw.writeline()别call其他function 测试一下
楼主: b26168 (b26168)   2016-04-18 17:45:00
好的 我再测试看看 结果怎样再回文~

Links booklink

Contact Us: admin [ a t ] ucptt.com