如果文件不存在,我需要读取我的代码。现在它正在读取是否存在创建和追加。密码如下:
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();
}
}