0

How can I generate log.txt file for the following code? I also want to see LIVE log being generated in C# application.

The below new.bat file runs the following command:

py file1.py -p COM# write_test 0x10000 device.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();

         }

I tried the below code to create logs but it doesn't work.

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");
                }
            }
PB stack
  • 1
  • 1
  • First, decide between `@` and ```\```. If you put the `@`, you don't need to make the double ` – Siegfried.V Nov 21 '22 at 19:53
  • I strongly recommend to lookup the (official) documentation for the ToShortTimeString() you are using to learn what exactly it outputs in which form. Then i would strongly suggest you print out the result of ToShortTimeString() into the console to see what it is for you (because the format of ToShortTimeString's result could perhaps be dependent on the regionale/culture settings you are using). Then, when looking at ToShortTimeString's result, ask yourself whether _all_ the characters that make up its result are actually valid characters for file names. If not, then you got your answer... –  Nov 21 '22 at 20:43
  • (And then there is also the matter of alternate data streams for files, a feature which is offered by file systems such as NTFS. Which means, depending on whether a `:` appears in your file name built with the ToShortTimeString() result, you actually might end up writing into an [alternate data stream](https://blog.foldersecurityviewer.com/ntfs-alternate-data-streams-the-good-and-the-bad/) of the file -- with a file name seemingly truncated compared to what you would expect it to be. Which can be confusing if you don't know about that feature of the NTFS file system...) –  Nov 21 '22 at 20:49

0 Answers0