gpt4 book ai didi

c# - 以编程方式安装服务而不打印调试信息

转载 作者:行者123 更新时间:2023-11-30 23:31:33 26 4
gpt4 key购买 nike

我正在使用此代码段以编程方式安装服务:

C#:

public static void InstallService(string filepath, 
string svcName,
string displayName = "",
string description = "",
ServiceStartMode startType = ServiceStartMode.Automatic,
ServiceAccount account = ServiceAccount.LocalSystem,
string username = "",
string password = "")
{
using (ServiceProcessInstaller installer = new ServiceProcessInstaller()) {

using (ServiceInstaller svc = new ServiceInstaller()) {

InstallContext context = new InstallContext("", { string.Format("/assemblypath={0}", filepath) });

installer.Account = account;
installer.Username = username;
installer.Password = password;

svc.Context = context;
svc.DisplayName = displayName;
svc.Description = description;
svc.ServiceName = svcName;
svc.StartType = startType;
svc.Parent = installer;

ListDictionary state = new ListDictionary();
svc.Install(state);
state.Clear();

}

}

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================

Vb.Net(原创):

Public Shared Sub InstallService(ByVal filepath As String,
ByVal svcName As String,
Optional ByVal displayName As String = "",
Optional ByVal description As String = "",
Optional ByVal startType As ServiceStartMode = ServiceStartMode.Automatic,
Optional ByVal account As ServiceAccount = ServiceAccount.LocalSystem,
Optional ByVal username As String = "",
Optional ByVal password As String = "")

Using installer As New ServiceProcessInstaller

Using svc As New ServiceInstaller

Dim context As New InstallContext("", {String.Format("/assemblypath={0}", filepath)})

installer.Account = account
installer.Username = username
installer.Password = password

svc.Context = context
svc.DisplayName = displayName
svc.Description = description
svc.ServiceName = svcName
svc.StartType = startType
svc.Parent = installer

Dim state As New ListDictionary
svc.Install(state)
state.Clear()

End Using

End Using

End Sub

问题是当调用那个方法时,安装状态写在应用程序的调试控制台中:

Installing service...

Service has been successfully installed.

Creating EventLog source in log Application...

我不确定这些类的哪些成员以何种方式请求打印该信息,只是它可能会禁用不需要的冗长信息吗?

最佳答案

在实例化 InstallContext 时使用 LogToConsole=false 命令行参数...

InstallContext context = new InstallContext("", { string.Format("/assemblypath={0} /LogToConsole=false", filepath) });

引用资料:

https://msdn.microsoft.com/en-us/library/system.configuration.install.installcontext_properties%28v=vs.110%29.aspx

https://msdn.microsoft.com/en-us/library/50614e95%28v=vs.110%29.aspx

关于c# - 以编程方式安装服务而不打印调试信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34557212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com