分享:第一次写Windows服务的几点心得(C#/VS2003)
如果对以个问题,您有更好的实现方法,请不吝赐教,谢谢!
第一次写Windows服务,虽说只是一个小程序,但也够我忙活了几天。本来前段时间就要写的,后来有些其他的事情,给耽搁了。在写这个程序的过程中,碰到了一些问题,现记录下来,希望对一些朋友有些帮助。
我做的这个服务是带界面的,其实服务跟界面是两个不同的项目,只是放在同一个解决方案下而已。
1、启动/停止服务
别看着好像挺简单,一两句代码就能搞定。
添加引用System.ServiceProcess.dll
ServiceController sc = new ServiceController(); sc.ServiceName = "服务名";
private void btnStart_Click(object sender, EventArgs e) { sc.Start();//启动 } private void btnStop_Click(object sender, EventArgs e) { sc.Stop();//停止 }
private void btnStart_Click(object sender, EventArgs e) { sc.Start();//启动 sc.WaitForStatus(ServiceControllerStatus.Running);//等待服务达到指定状态 }
private void btnStart_Click(object sender, EventArgs e) { if(sc.Status==ServiceControllerStatus.Stopped) sc.Start();//启动 sc.WaitForStatus(ServiceControllerStatus.Running);//等待服务达到指定状态 }
private void btnStart_Click(object sender, EventArgs e) { sc.Refresh();//刷新属性值 if(sc.Status==ServiceControllerStatus.Stopped) sc.Start();//启动 sc.WaitForStatus(ServiceControllerStatus.Running);//等待服务达到指定状态 }
using System.Configuration; string _value = ConfigurationSettings.AppSettings["Key值"];
using System.Xml; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(configPath);//configPath是config文件的路径,对于这个路径的获取,将会在后面说明 XmlNodeList nodes = xmlDoc.GetElementsByTagName("add"); Hashtable hash = new Hashtable(); foreach (XmlNode node in nodes) { hash.Add(node.Attributes["key"].Value.ToString(), node.Attributes["value"].Value.ToString()); } //通过hash["Key值"].ToString()就能获取到某一个key所对应的value了
XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load(configPath);//configPath为config文件的路径 XmlNode xmlNode=xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='"+_key+"']");//_key为需要修改其value的key值 xmlNode.Attributes["value"].InnerText=_value;//_value为新值 xmlDoc.Save(configPath);
using Microsoft.Win32; RegistryKey rk = Registry.LocalMachine; RegistryKey rkSub = rk.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\服务名"); string servicePath = rkSub.GetValue("ImagePath").ToString(); //那么我们就可以轻易地得到第2点所需要的config文件路径 string configPath = servicePath + ".config";
//This code should be inserted into your ProjectInstaller class' code
public override void Install(IDictionary stateServer)
{
Microsoft.Win32.RegistryKey system,
//HKEY_LOCAL_MACHINE\Services\CurrentControlSet
currentControlSet,
//...\Services
services,
//...\ <Service Name>
service,
//...\Parameters - this is where you can put service-specific configuration
config;
try
{
//Let the project installer do its job
base.Install(stateServer);
//Open the HKEY_LOCAL_MACHINE\SYSTEM key
system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
//Open CurrentControlSet
currentControlSet = system.OpenSubKey("CurrentControlSet");
//Go to the services key
services = currentControlSet.OpenSubKey("Services");
//Open the key for your service, and allow writing
service = services.OpenSubKey(this.BakServiceInstaller.ServiceName, true);
//Add your service's description as a REG_SZ value named "Description"
service.SetValue("Description", "你的描述写在这里!");
//(Optional) Add some custom information your service will use...
config = service.CreateSubKey("Parameters");
}
catch(Exception e)
{
Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
}
}
public override void Uninstall(IDictionary stateServer)
{
Microsoft.Win32.RegistryKey system,
currentControlSet,
services,
service;
try
{
//Drill down to the service key and open it with write permission
system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
currentControlSet = system.OpenSubKey("CurrentControlSet");
services = currentControlSet.OpenSubKey("Services");
service = services.OpenSubKey(this.BakServiceInstaller.ServiceName, true);
//Delete any keys you created during installation (or that your service created)
service.DeleteSubKeyTree("Parameters");
//...
}
catch(Exception e)
{
Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
}
finally
{
//Let the project installer do its job
base.Uninstall(stateServer);
}
}
partial class ProjectInstaller { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); // // serviceProcessInstaller1 // this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; // // serviceInstaller1 // this.serviceInstaller1.Description = "OOOOOOOOOOOOOOOOOOOOOOOOOOO";}
[解决办法]
标记一下 以后来看
[解决办法]
good
------解决方案--------------------
谢谢分享,收藏
[解决办法]
收藏
[解决办法]
占个位置,,学习
[解决办法]
mark
[解决办法]
收藏学习
[解决办法]
来学习 顶
[解决办法]
学习了,不错啊!!!
[解决办法]
多谢。
让我这个后进学徒又长见识咯
[解决办法]
UP
[解决办法]
qwrqreqwewq
[解决办法]
服务这块还没接触,以后再慢慢学习吧!
[解决办法]
顶上
[解决办法]
留个名
[解决办法]
学习
[解决办法]
之前刚做了一个监视系统,也是Service
[解决办法]
留个名先,有空在看
[解决办法]
帮顶,学习一下
[解决办法]
UP
[解决办法]
学习
[解决办法]
不错,先收藏
[解决办法]
先收藏,后学习
[解决办法]
谢谢楼主
[解决办法]
支持分享
[解决办法]
先收藏,后学习
[解决办法]
[code=C#][/code]
[解决办法]
收藏
[解决办法]
学习
[解决办法]
顶帖,学习一下
[解决办法]
回帖是一种美德!传说每天回帖即可获得 10 分可用分!
[解决办法]
帮顶
学习!
[解决办法]
学习了~~
[解决办法]
学习收藏
[解决办法]
ServiceInstaller a = new ServiceInstaller();
a.Description = "这是一个***服务";
这样可以添加服务控制类对象的描述。
也可以直接用ServiceInstaller 控件,在属性里面有设置
http://blog.csdn.net/xuhaiyan8825/archive/2009/01/10/3746953.aspx
这是我写的一篇服务的
包括服务本身,和服务控制,以及服务安装和卸载,还有调试
楼主可以看看,多提意见,我也是第一次写服务
------解决方案--------------------
呵呵 不错 学习了 顶一个
[解决办法]