【问题标题】:How to create logfilename.txt (text log file) with C# code below?如何使用下面的 C# 代码创建 logfilename.txt(文本日志文件)?
【发布时间】:2022-11-22 03:28:50
【问题描述】:

如何为以下代码生成 log.txt 文件?我还想查看在 C# 应用程序中生成的实时日志。

下面的 new.bat 文件运行以下命令:

py file1.py -p COM# write_test 0x10000 设备.bin


private void button1_Click_1(object sender, EventArgs e)
        {

            lbl_startingTest.Text = "Starting Test..";
            lbl_Result.Text = "Running";

            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "C:\\test\\test\\bin\\Debug\\new.bat";
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.WaitForExit();

         }

我尝试了以下代码来创建日志,但它不起作用。

string path = @"C:\\test\\logs" + System.DateTime.Now.ToShortTimeString() + ".txt";
            if (!File.Exists(path))
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("PB");
                    sw.WriteLine("device");
                    sw.WriteLine("Test");
                }
            }

【问题讨论】:

  • 首先,在@和\`. If you put the @, you don't need to make the double `之间做出选择

标签: python c# batch-file logging binary


【解决方案1】:

您缺少sw.Flush()。另外,检查以确保您有 else 语句。

只有当你调用 so.Flush() 时,你才真正在写作。截至目前,您的代码正在写入一个临时缓冲区,然后丢弃该缓冲区。

【讨论】:

    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多