Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows服务无法在Windows XP-C上正确运行bat文件#_C#_Batch File_Service_Windows Xp - Fatal编程技术网

C# Windows服务无法在Windows XP-C上正确运行bat文件#

C# Windows服务无法在Windows XP-C上正确运行bat文件#,c#,batch-file,service,windows-xp,C#,Batch File,Service,Windows Xp,我有一个windows服务,我希望它自动更新自己。 因此,当我的服务了解到存在更新时,它会创建一个.bat文件: File.WriteAllLines(updateFileName, new string[] {"sc stop "+ServiceName, "ping 10.10.10.10 -n 8", "DEL "+ServicePath, "RENAME "+NewServicePath+" "+ServicePath, "sc start "+ServiceN

我有一个windows服务,我希望它自动更新自己。 因此,当我的服务了解到存在更新时,它会创建一个.bat文件:

   File.WriteAllLines(updateFileName, new string[] {"sc stop "+ServiceName, 
    "ping 10.10.10.10 -n 8", "DEL "+ServicePath, 
    "RENAME "+NewServicePath+" "+ServicePath, "sc start "+ServiceName})
然后我创建一个流程并启动它,我已经尝试了这两种方法

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

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();
我还尝试编写一个控制台应用程序updateService.exe,用于停止、更新和启动服务。在本例中,我使用
serviceditory\updateService.exe
创建了一个.bat文件,并尝试运行它

我的服务使用
LocalSystem
帐户

以上所有这些在Windows7上都能很好地工作

但在Windows XP上它不起作用:

  • 服务停止,ping有效,del和rename命令无效, 服务不会再次启动
  • 服务使用updateservice.exe启动.bat文件,服务停止并再次启动,但services.exe文件不更新
    因此,我想知道如何强制我的服务在Windows XP上进行自我更新,我认为隐私有一些问题,但我不知道如何处理它们。

    首先,我建议在命令提示符窗口中手动运行创建的批处理文件,以查看服务运行时命令DELRENAME可能出现的问题

    其次,它没有发布变量
    newservicecepath
    ServicePath
    的值,这在这里可能很重要

    我建议进一步使用,而不是使用

    DEL ServicePath
    RENAME NewServicePath ServicePath
    
    批处理代码

    MOVE /Y "Name of new file with full path" "Name of file to update with full path"
    
    使用带有参数
    /Y
    的命令MOVE覆盖已存在的文件要比使用DELRENAME好得多,因为命令RENAME要求第二个参数仅为文件名而不包含路径。此外,使用MOVE仅更新一次后,服务文件才存在

    建议对这两个文件使用完整路径,因为在创建进程时或批处理文件内部都未设置批处理文件执行的当前目录


    使用
    cd/D“%~dp0”
    作为批处理文件的第一行,可以将批处理文件的当前目录设置为批处理文件的目录。

    明显的调试技术是将这些命令的输出重定向到文件,以便您可以找出它们失败的原因。您可能需要“2>”来捕获错误消息。也就是说,XP不受支持。@m我使用了输出到文件的选项,但里面什么都没有,文件保持空白。我知道XP已经不受支持了,但我们公司仍然使用近百台装有windows XP的计算机,我需要这项服务来为它们工作1。如果我手动启动创建的.bat文件,它运行得很好,而且一切都正常。2.所有路径都是正确的(我在创建文件后检查了它们),重命名仅用作第二个参数filename,而不是完整路径。我现在将尝试用MOVE/Y替换DEL和重命名,谢谢。我已将DEL和重命名更改为MOVE/Y,并提示:当我的服务启动时。bat文件命令
    sc stop
    ping
    正常工作,然后不执行任何命令
    MOVE/Y
    sc start
    。当我手动启动.bat文件时,它工作正常。所以我认为问题出在privileges上——但我的服务在LocalSystem下启动,并用
    Verb=“runas”
    启动.bat文件,我认为这一定足够了。。。