作者MarcAnthony (馬克)
看板C_Sharp
標題[問題] FileInfo and StreamWriter
時間Mon Sep 22 14:38:46 2008
下面這程式碼 如果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
1F:推 lostid:f.CreateText()的先後問題吧 09/22 15:24