简体   繁体   English

如何在C#中安装Windows服务(通过installutil)?

[英]How to install a Windows service (via installutil) in C#?

My code: 我的代码:

Process kurulum1 = new Process();
kurulum1.StartInfo.FileName = @"C:\ProgramData\Microsoft\Windows\StartMenu\Programs\Visual Studio 2017\Visual Studio Tools\Developer Command Prompt for VS 2017.lnk";

 kurulum1.StartInfo.Arguments = "cd C:\\Users\\stajyer3\\Documents\\Visual Studio 2017\\Projects\\TestWindowsService\\TestWindowsService\\bin\\Debug InstallUtil.exe "TestWindowService.exe"  ";

kurulum1.Start();

kurulum1.WaitForExit();

Not Working 无法运作

Error: 错误:

[ERROR:parse_cmd.bat] Invalid command line argument: 'cd'. [ERROR:parse_cmd.bat]无效的命令行参数:“ cd”。 Argument will be ignored. 参数将被忽略。 [ERROR:parse_cmd.bat] Invalid command line argument: 'C:\\Users\\stajyer3\\Documents\\Visual'. [ERROR:parse_cmd.bat]无效的命令行参数:'C:\\ Users \\ stajyer3 \\ Documents \\ Visual'。 Argument will be ignored. 参数将被忽略。 [ERROR:parse_cmd.bat] Invalid command line argument: 'Studio'. [ERROR:parse_cmd.bat]无效的命令行参数:“ Studio”。 Argument will be ignored. 参数将被忽略。 [ERROR:parse_cmd.bat] Invalid command line argument: '2017\\Projects\\TestWindowsService\\TestWindowsService\\bin\\Debug'. [ERROR:parse_cmd.bat]无效的命令行参数:'2017 \\ Projects \\ TestWindowsService \\ TestWindowsService \\ bin \\ Debug'。 Argument will be ignored. 参数将被忽略。


** Visual Studio 2017 Developer Command Prompt v15.0.26430.14 ** Copyright (c) 2017 Microsoft Corporation ********************************************************************** [ERROR:VsDevCmd.bat] * VsDevCmd.bat encountered errors. ** Visual Studio 2017开发人员命令提示符v15.0.26430.14 **版权所有(c)2017 Microsoft Corporation **************************** ********************************************** [ERROR:VsDevCmd.bat] * VsDevCmd.bat遇到错误。 Environment may be incomplete and/or incorrect. 环境可能不完整和/或不正确。 * *

you have made many mistakes here , with out knowing your objective its very difficult to say which is the worst 您在这里犯了很多错误,却不知道自己的目标,很难说哪个是最糟糕的

  1. ProcessStartInfo.FileName should be the file you want to execute not a start menu shortcut ProcessStartInfo.FileName应该是您要执行的文件,而不是开始菜单快捷方式

  2. ProcessStartInfo.Arguments is a command line argument not a command ProcessStartInfo.Arguments是命令行参数而不是命令

  3. the command you are using seems to be an attempt to change the working directory this should be done with ProcessStartInfo.WorkingDirectory 您正在使用的命令似乎是试图更改工作目录,这应该通过ProcessStartInfo.WorkingDirectory完成

  4. the strings you are using are broken because you are exiting the strings by using " but then you carry the string on, so i must assume you mean the " to be inside the string in which case you need to delimit them with \\ this would then look like "he said \\"delimit your strings\\" " though if you are using the @ notation then the delimiter becomes "" not \\" 您正在使用的字符串已损坏,因为您使用“”退出了字符串,但随后携带了该字符串,因此我必须假定您的意思是“”位于字符串内,在这种情况下,您需要用\\分隔它们看起来像"he said \\"delimit your strings\\" "但是如果您使用@表示法,则分隔符变为“”而不是\\”

  5. arguments are space separated strings so if your arguments contain spaces such as long form filenames then you need to surround them with quotes " so ProcessStartInfo.Arguments = "abc:\\\\temp"; will pass the args "a", "b" and "c:\\temp" to the executing program but ProcessStartInfo.Arguments = "a literal string arg"; would pass each word as as separate argument ProcessStartInfo.Arguments = "\\"a literal string arg\\""; and this will pass in a single arg with the entire string 参数是用空格分隔的字符串,因此,如果您的参数包含空格(例如长格式文件名),则需要用引号将其引起来,以便ProcessStartInfo.Arguments = "abc:\\\\temp";将传递参数“ a”,“ b”和“ c:\\ temp”到正在执行的程序,但是ProcessStartInfo.Arguments = "a literal string arg";会将每个单词作为单独的参数传递ProcessStartInfo.Arguments = "\\"a literal string arg\\"";并且它将传入具有整个字符串的单个arg

您需要将路径用引号引起来(并在文件名中使用引号引起来)

kurulum1.StartInfo.Arguments = "cd \"C:\\Users\\stajyer3\\Documents\\Visual Studio 2017\\Projects\\TestWindowsService\\TestWindowsService\\bin\\Debug InstallUtil.exe\" \"TestWindowService.exe\"  ";

Give this a whirl: 旋转一下:

var kurulum1 = new Process
{
    StartInfo =
    {
        FileName = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe",
        WorkingDirectory =
            "C:\\Users\\stajyer3\\Documents\\Visual Studio 2017\\Projects\\TestWindowsService\\TestWindowsService\\bin\\Debug",
        Arguments = "TestWindowService.exe"
    }
};
kurulum1.Start();
kurulum1.WaitForExit();

There is no need to involve the Developer Command Prompt - you can just invoke installutil directly. 无需涉及Developer Command Prompt -您可以直接调用installutil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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