2014-01-09 128 views
0

我有一个名为MyCustomUpdater.exe的程序,下载Windows服务zip文件并将其解压缩。如何从C#安装Windows服务(桌面应用程序)

提取文件夹中有MyWindowsServices.exe文件

我想从MyCustomUpdater.exe

安装这些MyWindowsServices.exe

我用波纹管提到的代码为它

Process process = new Process(); 
process.StartInfo.FileName = "cmd.exe"; 
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
process.StartInfo.RedirectStandardInput = true; 
process.StartInfo.RedirectStandardError = true; 
process.StartInfo.RedirectStandardOutput = true; 
process.StartInfo.UseShellExecute = false; 
process.StartInfo.Verb = "runas"; 
process.Start(); 

if (process != null) 
{ 
    process.StandardInput.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.Windows) + "\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe /u \"" + "E:\Testing\MV.AutoUpdateWindowsService.exe" + "\""); 
    process.StandardInput.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.Windows) + "\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe \"" + "E:\Testing\MV.AutoUpdateWindowsService.exe" + "\""); 
    process.StandardInput.Close(); 
} 
process.Close(); 

但它不会安装windows服务

这段代码只是简单地打开一个空白的黑色命令提示窗口;没有进一步执行。

我也尝试在

process.StartInfo.Arguments 

运行Windows服务安装命令我用点网框架4.5和Visual Studio 2013


我添加app.manifest文件我MyCustomUpdater.exe 在包含app.manifest之后,我设置了它。 右键单击程序>属性>应用程序>资源>清单[点击下拉] 并选择app.manifest 我机会代码之后

Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Microsoft.NET\Framework\v4.0.30319\installutil.exe", _FilePath); 

这工作得很好 但是,对于卸载过程

Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Microsoft.NET\Framework\v4.0.30319\installutil.exe", "/u " + _FilePath); 

Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Microsoft.NET\Framework\v4.0.30319\installutil.exe" + " /u ", _FilePath); 

不工作

贝娄是app.manifest文件

<?xml version="1.0" encoding="utf-8"?> 
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <assemblyIdentity version="1.0.0.0" name="MyApplication.app" /> 
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> 
    <security> 
     <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> 
     <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 
     </requestedPrivileges> 
     <applicationRequestMinimum> 
     <PermissionSet Unrestricted="true" ID="Custom" SameSite="site" /> 
     <defaultAssemblyRequest permissionSetReference="Custom" /> 
     </applicationRequestMinimum> 
    </security> 
    </trustInfo> 
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
    <application> 
    </application> 
    </compatibility> 
</asmv1:assembly> 

帮助我的代码。

感谢

+0

嗨。是否有一个原因,你想使用标准输入而不是直接执行installutil.exe(即在“StartInfo.FileName”中)将exe传递给'cmd.exe'? – Rob

+0

我添加了'process.OutputDataReceived + =(s,e)=> _ILog.Fatal(@“OutputDataReceived:”+ e.Data);'也不能得到输出 –

+0

它是如何失败? 'InstallUtil.InstallLog'文件中是否有任何线索? – Rob

回答

0

这似乎有点不寻常的使用cmd.exe在这样的时候,你可以简单地直接执行安装可执行文件。但是如果你真的想要使用cmd.exe和STDIN重定向。那么您需要通过传递“/ K”开关来使用交互模式。

有关更多详细信息,请参阅此answer

+0

我添加** app.manifest **以管理员模式运行我的应用程序。之后'Process.Start'工程安装。 –

0

您应该以管理员身份运行试图拨打installutil.exe的程序,然后直接运行installutil.exe,不需要cmd的帮助。此外,我发现PInvoke更稳定(请参阅OpenSCManager,CreateService)。

0

它做

随着app.manifest程序的帮助运行在管理员模式。 [代码如上]

Bellow是在“无声”模式下安装windows服务的代码。

Process _Process = new Process(); 
_Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
_Process.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Microsoft.NET\Framework\v4.0.30319\installutil.exe"; 
_Process.StartInfo.Arguments = "/u \"" + _FilePath + "\""; 
_Process.Start(); 
System.Threading.Thread.Sleep(8000); 
_Process.Close(); 
_Process = new Process(); 
_Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
_Process.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Microsoft.NET\Framework\v4.0.30319\installutil.exe"; 
_Process.StartInfo.Arguments = _FilePath; 
_Process.Start(); 
_Process.Close(); 

谢谢大家。

相关问题