[問題] streamwriter reader在不同環境下不work

看板C_Sharp (C#)作者 (森)時間9年前 (2016/04/12 11:39), 9年前編輯推噓8(8014)
留言22則, 4人參與, 最新討論串1/1
使用 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(); 不知道該從何下手.. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.87.85.162 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1460432399.A.935.html

04/12 12:08, , 1F
所以我說那個code勒
04/12 12:08, 1F

04/12 12:13, , 2F
至少把你streamwriter做事部分的codePO出來吧
04/12 12:13, 2F
OK~ PO個大概的 ※ 編輯: b26168 (219.87.85.162), 04/12/2016 12:24:22

04/12 12:43, , 3F
這個code看起來 就算沒讀到檔
04/12 12:43, 3F

04/12 12:43, , 4F
應該還是會有寫入東西
04/12 12:43, 4F

04/12 12:45, , 5F
可是為什麼你讀寫的是同一個檔案...?
04/12 12:45, 5F

04/12 16:51, , 6F
這個檔案要接下去寫下去 主要的不是這個檔拉 這是debug用
04/12 16:51, 6F

04/12 16:52, , 7F
自己電腦試這段code是沒問題的 想問是否環境有關?
04/12 16:52, 7F

04/12 18:43, , 8F
Close 之前,先Flush
04/12 18:43, 8F

04/12 18:44, , 9F
可能你讀的東西太少,還在buffer
04/12 18:44, 9F

04/12 18:54, , 10F
不用 close會自動flush
04/12 18:54, 10F

04/12 23:25, , 11F
問題是我跟對方電腦讀的檔案內容一樣 寫出一樣 對方卻不行
04/12 23:25, 11F

04/13 09:16, , 12F
不是阿 我怎麼看你讀檔寫檔的路徑都一樣阿
04/13 09:16, 12F

04/13 09:16, , 13F
如果最後出來是0kb 代表你讀的檔案本身就是沒東西
04/13 09:16, 13F

04/13 09:16, , 14F
因為是同一個阿..........
04/13 09:16, 14F

04/13 12:24, , 15F
如果你讀出str沒有要做其他事,根本不需要讀出str,請用
04/13 12:24, 15F


04/13 12:27, , 17F
照理說也不應該空白 只能試試看在close之前先sw.flush()
04/13 12:27, 17F

04/13 12:33, , 18F
這的確是匪夷所思,能建應該就能寫,除非他寫之前就跳錯了
04/13 12:33, 18F

04/13 12:33, , 19F
例如str是null或GetNowDateString()出錯
04/13 12:33, 19F

04/13 13:42, , 20F
摁 那建議原po先單純測試sw.writeline()
04/13 13:42, 20F

04/13 13:42, , 21F
別call其他function 測試一下
04/13 13:42, 21F

04/18 17:45, , 22F
好的 我再測試看看 結果怎樣再回文~
04/18 17:45, 22F
文章代碼(AID): #1N36uFar (C_Sharp)
文章代碼(AID): #1N36uFar (C_Sharp)