求解,怎么样在winform中注册、启动、停止用c#写的windows服务

jimmy-yu 2008-07-05 11:05:00
我想在winform中实现修改windows服务的配置文件(比如我将时间及数据库链接的配置写在app.config中,在winform中动态加载这个config,然后修改其中的节点),修改完成后再注册这个服务程序,同时提供两个按钮作为启动或停止,以用来启动或停止这个服务,这个应该怎么实现啊,哪位高手能帮帮我啊!提前先谢谢了!
...全文
245 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
活靶子哥哥 2008-07-05
  • 打赏
  • 举报
回复
Install
注册服务和卸载服务可以用Process调用如下命令

%windir%\Microsoft.NET\Framework\v2.0.50727\installutil /i YourServiceExe.exe
net start "You Service Name"

UnInstall
%windir%\Microsoft.NET\Framework\v2.0.50727\installutil /u YourServiceExe.exe


see also http://msdn.microsoft.com/zh-cn/architecture/50614e95.aspx

System.ServiceProcess.ServiceController sc = new
System.ServiceProcess.ServiceController("Your Service Name");
sc.Start//启动
sc.Stop()//停止;
sc.Pause()//暂停
sc.Continue()//继续

see alse http://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicecontroller_methods.aspx
tianhuo_soft 2008-07-05
  • 打赏
  • 举报
回复
winform 里面不是有一个专门制作服务的功能嘛
用那个多好啊 ~!

wwshiwojiaguaiguai 2008-07-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zzyhuian06142 的回复:]
用Process类啊
设置他的StartInfo.Arguments
[/Quote]
jimmy-yu 2008-07-05
  • 打赏
  • 举报
回复
同时再请教下,怎么样把windows服务和winform一起打包成安装文件
xminsong 2008-07-05
  • 打赏
  • 举报
回复
资料来自网上,希望对你有用

static void Main(string[] args)
{
if (args.Length == 0)
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun);
}
else if (args[0].ToLower() == "-i")
{
InstallService();
}

//如参数为"-u",删除服务
else if (args[0].ToLower() == "-u")
{
UnInstallService();
}
}

//安培服务
string[] commandLine = { };
string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
AssemblyInstaller aAssemblyInstaller = new AssemblyInstaller(serviceFileName, commandLine);
TransactedInstaller aTransactedInstaller = new TransactedInstaller();
aTransactedInstaller.Installers.Add(aAssemblyInstaller);
aTransactedInstaller.Install(new System.Collections.Hashtable());

//删除服务
string[] commandLine = { };
string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
AssemblyInstaller aAssemblyInstaller = new AssemblyInstaller(serviceFileName, commandLine);
TransactedInstaller aTransactedInstaller = new TransactedInstaller();
aTransactedInstaller.Installers.Add(aAssemblyInstaller);
aTransactedInstaller.Uninstall(null);
jimmy-yu 2008-07-05
  • 打赏
  • 举报
回复
能给个具体的例子吗
或者稍微详细点的代码
谢谢
zzyhuian06142 2008-07-05
  • 打赏
  • 举报
回复
用Process类啊
设置他的StartInfo.Arguments
jimmy-yu 2008-07-05
  • 打赏
  • 举报
回复
在vs命令行注册我知道,但是不知道在winform怎么注册,比如说放一个按钮,然后在按钮里写一个事件就可以完成注册这样

110,017

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧