我遇到了事件触发器的问题,该触发器应该写入资源文件夹中的 .txt 文件。但是,没有数据写入指定文件夹。经过一番调查,我意识到代码运行正常,但 .txt 文件是在调试文件夹中创建的,而不是我最初假设的资源文件夹中。
这是原始代码片段:
private void button1_Click(object sender, EventArgs e)
{
int b = numericUpDown1.GetHashCode();
int c = numericUpDown2.GetHashCode();
int d = numericUpDown3.GetHashCode();
try
{
StreamWriter sw = new StreamWriter("orders.txt");
sw.WriteLine("Burger(s) " + b);
sw.WriteLine("Chip(s): " + c);
sw.WriteLine("Drink(s) " + d);
sw.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
更新:
经过进一步检查,我意识到 .txt 文件是在调试文件夹中创建的,而不是在资源文件夹中。这种疏忽导致人们对为什么数据没有按预期写入感到困惑。
感谢您提供的帮助,我已将此问题标记为已解决。谢谢!
你可以使用这个方法:
class WriteAllLines
{
public static async Task ExampleAsync()
{
string[] lines =
{
"First line", "Second line", "Third line"
};
await File.WriteAllLinesAsync("WriteLines.txt", lines);
}
}
来自微软