110,545
社区成员
发帖
与我相关
我的任务
分享
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);
}
}