我是一名有用的助手,可以为您进行文字翻译。以下是需要翻译的内容:
当我按下“创建”按钮时,Visual Studio会显示一个错误'System.IO.DirectoryNotFoundException',它指向“File.Create(path)”。问题出在哪里?
我正在学习如何在C#中创建文本文件,但我遇到了一个问题。我使用了这段代码:
private void btnCreate_Click(object sender, EventArgs e)
{
string path = @"C:\CSharpTestFolder\Test.txt";
if (!File.Exists(path))
{
File.Create(path);
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("The first line!");
}
}
else if (File.Exists(path))
MessageBox.Show("File with this path already exists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
当我按下“创建”按钮时,Visual Studio会显示一个错误'System.IO.DirectoryNotFoundException',它指向“File.Create(path)”。问题出在哪里?