파일이 없으면 코드를 읽어야 다른 파일을 만들 수 있습니다. 지금 그것이 존재한다면 그것은 독서이며, 창조하고 덧붙입니다. 코드는 다음과 같습니다.파일이없는 경우 파일 만들기
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();
}
}이 파일 이 존재하지 않는 경우 확인하려면
[File.AppendAllText] (HTTP : // MSDN .microsoft.com/ko-kr/library/ms143356.aspx) -이 코드는 한 줄의 코드에서만 필요합니다. –
@ShadowWizard 이후 th 숙제 OP가 실제로 조건부 논리를 표시하도록 지시 될 수 있습니다. – Yuck
@Yuck - 바퀴를 재발 명하기위한 숙제? 왝! ;) –