0

I have written a code in C#. The code is loging into a file the following:

  1. CPU Usage
  2. RAM Usage
  3. Network card traffic
  4. Current time

I want that on system startup the code will be executed automatically. So I have written a batch file that runs the .exe file like that:

START 'C:\Debug\DiagnisticTool.exe'

while DiagnisticTool.exe is the application and it is contained in a folder named 'Debug' at C drive. The batch file is in the windows startup folder.

While I start my PC the batch file is running and than the error stated above comes up. This is how the file is been written in the script code:

using (StreamWriter outputFile = File.AppendText("PerformanceLogFile.csv"))
                {
                    outputFile.WriteLine(something to write to file);
                }

yet the error stateds that it tries to write it somewere else: 'C:\Windows\system32'.

Why is that?

When I run the program via visual studio or via the .exe application that it created it is runing OK. Plus when I run the batch file manually it works fine. The ONLY problem is that it doesn't run at startup as it should.

Twins96
  • 15
  • 5
  • Specify the full path of that file, not just its name. The *CurrentDirectory* will change. – Jimi Mar 22 '20 at 11:48
  • Windows and `cmd.exe` does not treat single quotes as special characters; you can use them within filenames. So in this instance you should use, `START C:\Debug\DiagnisticTool.exe` or better `START "" "C:\Debug\DiagnisticTool.exe"`, _(where the empty doublequtes can be filled with your chosen title)_. – Compo Mar 22 '20 at 12:15
  • If you need the executable to run with a particular directory as the current directory, which is different from that which is currently defined, you must change that directory first using either `CD /D "C:\Debug"` or `PushD "C:\Debug"` or define it with `Start`'s `/D` option, `Start /D "C:\Debug" "C:\Debug\DiagnisticTool.exe"`. To find out how each of the commands work, you can open a Command Prompt window and enter, `start /?`, `cd /?`, `pushd /?` etc. _As you can see from the above, it is always good practice to doublequote filenames whether relative or absolute._ – Compo Mar 22 '20 at 12:21
  • Additionally, the only files you should add to the startup directory should be shortcuts. In this case you should be adding a shortcut to your batch file, not the batch file itself. It is therefore worth noting that for the task you've implied in your question, you don't need a batch file at all, you can simply place a shortcut to `C:\Debug\DiagnisticTool.exe` inside the startup directory instead. Within the properties of that shortcut, you can, if necessary, include any additional options/arguments, and the 'start in' directory too. – Compo Mar 22 '20 at 12:31
  • Aside from the above, my suggestion would be to use the full path for your output/append file within the C# code itself, then place a shortcut to that C# in the startup directory, _as advised in my previous comment_. Finally, you are aware that your named executable is incorrectly spelled, it should be `Diagnostic` not `Diagnistic`. – Compo Mar 22 '20 at 12:39

1 Answers1

0

This happens because when your program is called by the batch the current folder is not the one you have your executable and windows doesn't care to change the current directory to your folder. The only thing that matters is the availability of your program and this is granted because you give the full path to the START command.

You can fix your problem changing the START command and specifying the folder where your program should be run

START /D C:\Debug C:\Debug\DiagnisticTool.exe

or adding a CD command just before running the file

CD C:\DEBUG
START C:\DEBUG\DiagnosticTool.exe

or changing the path passed to StreamWriter

using (StreamWriter outputFile = File.AppendText(@"C:\debug\PerformanceLogFile.csv"))

this last option, while allowing to leave your batch as is, could be a problem if you want to customize the output folder. For example if you are not allowed to create a C:\DEBUG folder on the target machine or for some other reason.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • I have changed it as you suggested:`'START /D C:\Debug 'C:\Debug\DiagnisticTool.exe` but it does not work. The CMD window starts and immediately closes @Steve – Twins96 Mar 22 '20 at 12:00
  • Not sure what your program is doing at start. Try to use this command line as a way to see if there is some error message on the console. _start /D C:\DEBUG cmd.exe /K C:\DEBUG\DiagnosticTools.exe_ – Steve Mar 22 '20 at 12:06
  • you mean to write it into the batch file? – Twins96 Mar 22 '20 at 12:09
  • Yes, it should open a second console and keep it open even after the tool exits – Steve Mar 22 '20 at 12:10
  • Actually now your second suggestion did work: `CD C:\DEBUG START C:\DEBUG\DiagnosticTool.exe`. But why was there a need in changing the CMD directory? – Twins96 Mar 22 '20 at 12:15