我想修改应用程序的路径,但这样做会破坏它,因为服务仍然指向旧的位置。

通过转到管理工具>服务,您可以打开一个属性对话框并查看可执行文件的路径,但没有办法更改它。

用户是否可以在不重新安装应用程序的情况下修改服务路径?


当前回答

你也可以用PowerShell:

Get-WmiObject win32_service -filter "Name='My Service'" `
    | Invoke-WmiMethod -Name Change `
    -ArgumentList @($null,$null,$null,$null,$null, `
    "C:\Program Files (x86)\My Service\NewName.EXE")

Or:

Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\My Service" `
    -Name ImagePath -Value "C:\Program Files (x86)\My Service\NewName.EXE"

其他回答

不能直接编辑执行服务的路径。你可以使用sc命令,

SC CONFIG ServiceName binPath= "Path of your file"

Eg:

sc config MongoDB binPath="I:\Programming\MongoDB\MongoDB\bin\mongod.exe --config I:\Programming\MongoDB\MongoDB\bin\mongod.cfg --service"

您可以删除该服务:

sc delete ServiceName

然后重新创建服务。

如果安装了Process Hacker,就可以使用它。

你也可以用PowerShell:

Get-WmiObject win32_service -filter "Name='My Service'" `
    | Invoke-WmiMethod -Name Change `
    -ArgumentList @($null,$null,$null,$null,$null, `
    "C:\Program Files (x86)\My Service\NewName.EXE")

Or:

Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\My Service" `
    -Name ImagePath -Value "C:\Program Files (x86)\My Service\NewName.EXE"

使用Invoke-WmiMethod的替代方法是使用更新的CIM cmdlet。这也避免了@($null,$null…)对象的需要,就像在前面的回答中看到的那样。

Get-CimInstance win32_service -Filter "Name='My Service'" | Invoke-CimMethod -MethodName - Change -Arguments @{PathName="C:\Program Files\My Service\NewName.exe"}