事件触发器未写入资源文件夹中的 .txt 文件 - 问题已解决

发布于 2025-01-14 16:27:59 字数 793 浏览 1 评论 0原文

我遇到了事件触发器问题,该触发器应该写入资源文件夹中的 .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 文件是在调试文件夹中创建的,而不是在资源文件夹中创建的。这种疏忽导致人们对为什么数据没有按预期写入感到困惑。

感谢您提供的帮助,我已将此问题标记为已解决。谢谢你!

I encountered an issue with an event trigger that was supposed to write to a .txt file within my resource folder. However, no data was being written to the designated folder. After some investigation, I realized that the code was functioning correctly, but the .txt file was being created in the debug folder, not the resource folder as I initially assumed.

Here's the original code snippet:

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);
    }
}

Update:

Upon further inspection, I realized the .txt file was being created in the debug folder, not the resource folder. This oversight led to confusion regarding why the data wasn't being written as expected.

I appreciate the assistance provided, and I've marked this issue as resolved. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2025-01-21 16:27:59

您可以使用以下方法:

class WriteAllLines
{
  public static async Task ExampleAsync()
{
    string[] lines =
    {
        "First line", "Second line", "Third line" 
    };

    await File.WriteAllLinesAsync("WriteLines.txt", lines);
}

来自

microsoft

此处

You can use this method:

class WriteAllLines
{
  public static async Task ExampleAsync()
{
    string[] lines =
    {
        "First line", "Second line", "Third line" 
    };

    await File.WriteAllLinesAsync("WriteLines.txt", lines);
}

}

from microsoft

Here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文