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

2.6k views Asked by At

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

3

There are 3 answers

0
adt On BEST ANSWER

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.

0
Will Dean On

Unless you're working for Microsoft, don't create folders in windows\system32.

0
jglouie On

Is your batch script using relative or absolute paths?

If relative, change the current working directory. This can be set via ProcessStartInfo. See MSDN info: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx

The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.

Also consider writing to %temp% as suggested by Blogbeard.

(post your batch script please)