简体   繁体   中英

Windows Service cannot run a bat file properly on Windows XP - C#

I have a windows service, and I want it to automaticly update itself. So, when my service understands, that there is update, it creates a .bat file:

   File.WriteAllLines(updateFileName, new string[] {"sc stop "+ServiceName, 
    "ping 10.10.10.10 -n 8", "DEL "+ServicePath, 
    "RENAME "+NewServicePath+" "+ServicePath, "sc start "+ServiceName})

Then I create a process and start it, I have tried both this

Process updateProcess = new Process();
updateProcess.StartInfo.FileName = updateFileName;
updateProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
updateProcess.StartInfo.CreateNoWindow = true;
updateProcess.StartInfo.Verb = "runas";
updateProcess.Start();

and this

var psi = new ProcessStartInfo
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                FileName = "cmd.exe",
                Arguments = "/C " + updateFileName,
                WindowStyle = ProcessWindowStyle.Hidden,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                Verb = "runas",
                ErrorDialog = false
            };

            updateProcess.StartInfo = psi;
            updateProcess.Start();

I have also tried to write a console application updateService.exe that stops, updates and starts service. In this case I've created a .bat file with ServiceDirectory\\updateService.exe and also tried to run it.

My service using LocalSystem account.

All of this things above work perfectly fine on Windows 7.

But on Windows XP it doesn't work:

  1. Service stops, ping works, del and rename commands doesn't work, service doesn't start again.
  2. Service starts .bat file withupdateService.exe, service stops and starts again, but services .exe file doesn't update.

So, I want to know how can I force my service to update itself on Windows XP, I think there is some issues with priveleges, but I don't know what to do with them.

First, I suggest to run the created batch file manually from within a command prompt window to see possible problems with command DEL and RENAME while the service is running.

Second, it was not posted the values of the variables NewServicePath and ServicePath which might be important here.

I suggest further using instead of

DEL ServicePath
RENAME NewServicePath ServicePath

the batch code

MOVE /Y "Name of new file with full path" "Name of file to update with full path"

Using command MOVE with parameter /Y to overwrite already existing file is much better than using DEL and RENAME as command RENAME requires that second parameter is only the file name without path. And additionally the service file exists after update only once with using MOVE .

Using full path for both files is recommended as current directory for batch file execution is not set on creating the process nor inside the batch file.

With using cd /D "%~dp0" as first line in batch file it would be possible to set the current directory for the batch file to directory of the batch file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM