作者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