Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Windows service executes batch file from system32 folder not install folder

The code for the service is as follows

            System.Diagnostics.Process proc = new System.Diagnostics.Process(); // Declare New Process

            var arguments =
                String.Format("--ip {0}  --user {1} --passwd {2} --guest {3} --gpasswd {4} --action {5}",
                              controllerIPAddress, controllerUsername, controllerPassword, username, password, action);

            proc.StartInfo.Arguments = arguments;

            proc.StartInfo.FileName = "C:\\Program Files\\Netspot\\ControllerInterfaceService\\batchfile.bat";

            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.Start();
            proc.WaitForExit();

I have a windows service that runs a dos script that calls a WGET command, all good, but I need to create and delete a temp folder using the batch script.

The problem I have is that the service is mapping the path to

  c:\windows\system32

instead of

  C:\\Program Files\\Netspot\\ControllerInterfaceService\\

This works fine within a test harness.

Any ideas on why the service uses the system32 folder instead of mapping to the local folder

like image 206
Welsh King Avatar asked Sep 19 '25 10:09

Welsh King


1 Answers

By default current directory for windows service is System32.

This link might be helpfull:

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

Use the above line of code to set the current directory to the same directory as your windows service.

like image 189
adt Avatar answered Sep 21 '25 01:09

adt