[問題] FileInfo and StreamWriter

看板C_Sharp作者 (馬克)時間17年前 (2008/09/22 14:38), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
下面這程式碼 如果file_path不存在 就會有exception出現 public void write(String file_path) { FileInfo f = new FileInfo(file_path); if (f.Exists) { ;//do nothing } else { f.Create(); } StreamWriter sw = f.CreateText(); sw.Write("hello world"); sw.Flush(); sw.Close(); } 若改成這樣 就ok了 public void write(String file_path) { FileInfo f = new FileInfo(file_path); StreamWriter sw = f.CreateText(); sw.Write("hello world"); sw.Flush(); sw.Close(); if (f.Exists) { ;//do nothing } else { f.Create(); } } 其實就是if else放在下面 那為什麼第一段code會有問題呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.39.212.210

09/22 15:24, , 1F
f.CreateText()的先後問題吧
09/22 15:24, 1F
文章代碼(AID): #18rprtyj (C_Sharp)