如果文件不存在,我需要让我的代码读取创建其他附加。现在它正在读取它是否存在创建和附加。这是代码:
if (File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
我会这样做吗?
if (! File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
编辑:
string path = txtFilePath.Text;
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
foreach (var line in employeeList.Items)
{
sw.WriteLine(((Employee)line).FirstName);
sw.WriteLine(((Employee)line).LastName);
sw.WriteLine(((Employee)line).JobTitle);
}
}
}
else
{
StreamWriter sw = File.AppendText(path);
foreach (var line in employeeList.Items)
{
sw.WriteLine(((Employee)line).FirstName);
sw.WriteLine(((Employee)line).LastName);
sw.WriteLine(((Employee)line).JobTitle);
}
sw.Close();
}
}