如何开发一个服务项目应用程序?关于 windows service 相关的 我想用VS新建一个项目,但不知如何写代码,让编译好的EXE可以直接运行安装服务,而不是通过.net命令行来安装。如果通过.net 命令行安装的话比较好做,但不知服务的名称和服务的描述在什么地方可以改?希望各位朋友能提供详细代码或操作过程。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用C#代码安装 Windows 服务(不使用InstallUtil) using System;using System.Collections.Generic;using System.ServiceProcess;using System.Configuration.Install;static class Program{ /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // 运行服务 if (args.Length == 0) { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MyService1() }; ServiceBase.Run(ServicesToRun); } // 安装服务 else if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i") { try { string[] cmdline = { }; string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location; TransactedInstaller transactedInstaller = new TransactedInstaller(); AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline); transactedInstaller.Installers.Add(assemblyInstaller); transactedInstaller.Install(new System.Collections.Hashtable()); } catch (Exception ex) { string msg = ex.Message; } } // 删除服务 else if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u") { try { string[] cmdline = { }; string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location; TransactedInstaller transactedInstaller = new TransactedInstaller(); AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline); transactedInstaller.Installers.Add(assemblyInstaller); transactedInstaller.Uninstall(null); } catch (Exception ex) { string msg = ex.Message; } } }} 新建WindowsService----添加安装程序---ProjectInstaller.cs----serviceInstaller1-----ServiceName 采用设计模式究竟有什么好处? 疑问求解! C# Socket 判斷消息已被接受 奇怪的错误!!请高手解答。高分回报!! 新手请教:帮忙改下这个登录程序的错误! 不用正则表达式和TextChange事件的话如何实现C/S里输入框只能输入数字? 请教各位大侠:asp.net使用c#如何操作access数据库 获取一段字符串后有无办法将字符串按照每行20个字符这样输出呢? 高手进来接分.来者有份 各位大虾,异或问题,请帮忙 一个窗体显示另一个窗体内容的问题~~ 谁能帮我解释下这个代码是什么意思啊
using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Configuration.Install;static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
// 运行服务
if (args.Length == 0)
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyService1() };
ServiceBase.Run(ServicesToRun);
}
// 安装服务
else if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i")
{
try
{
string[] cmdline = { };
string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location; TransactedInstaller transactedInstaller = new TransactedInstaller();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
transactedInstaller.Installers.Add(assemblyInstaller);
transactedInstaller.Install(new System.Collections.Hashtable());
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
// 删除服务
else if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u")
{
try
{
string[] cmdline = { };
string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location; TransactedInstaller transactedInstaller = new TransactedInstaller();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
transactedInstaller.Installers.Add(assemblyInstaller);
transactedInstaller.Uninstall(null);
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
}
}