C#创建系统服务 用c#创建windows服务,然后创建按照程序,安装以后程序是停止的,怎么设置让按照以后就启动服务。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 执行DOS脚本“net start 服务名”即可开启PS:其实连服务安装都是可以通过DOS脚本来执行的,因此整个过程写个批处理比较好。至于批处理文件,你可以包含在项目中,也可以让程序在临时目录创建个临时的bat文件,写入脚本内容,之后保存调用执行。 用C#创建Windows服务这个很全面,启动服务的方法什么的都有讲解。启动服务 win7下用c#开发windows服务设置StartType属性(启动服务的方式,分为手动、自动和禁用) using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.ServiceProcess; using System.Text;using System.Windows.Forms;namespace EAssistantMonitorServiceManage{ public partial class FrmMain : Form { string serviceName = "监控服务名称"; public FrmMain() { InitializeComponent(); TimerState.Enabled = true; } private void BtnStartService_Click(object sender, EventArgs e) { try { ServiceController scDBService = new ServiceController(serviceName); ServiceController[] scAllService = ServiceController.GetServices(); int i = scAllService.Length - 1; while (i > 0) { if (scAllService[i].DisplayName == serviceName) { if (scDBService.Status.Equals(ServiceControllerStatus.Stopped)) { State = serviceName + " 正在启动..."; scDBService.Start(); } else if (scDBService.Status.Equals(ServiceControllerStatus.Running)) { State = serviceName + " 正在运行..."; } if (!TimerState.Enabled) TimerState.Start(); return; } else { i--; } } State = serviceName + " 并没有安装在本机上,请检查。"; if (TimerState.Enabled) TimerState.Stop(); } catch (Exception exp) { State = serviceName + " 启动失败,原因:" + exp.Message; } } private void BtnStopService_Click(object sender, EventArgs e) { try { ServiceController scDBService = new ServiceController(serviceName); ServiceController[] scAllService = ServiceController.GetServices(); int i = scAllService.Length - 1; while (i > 0) { if (scAllService[i].DisplayName == serviceName) { if (scDBService.Status.Equals(ServiceControllerStatus.Running)) { State = serviceName + " 正在停止..."; scDBService.Stop(); } else if (scDBService.Status.Equals(ServiceControllerStatus.Stopped)) { State = serviceName + " 已经停止..."; } if (TimerState.Enabled) TimerState.Start(); return; } else { i--; } } State = serviceName + " 并没有安装在本机上,请检查。"; if (TimerState.Enabled) TimerState.Stop(); } catch (Exception exp) { State = serviceName + " 启动失败,原因:" + exp.Message; } } /// <summary> /// 服务状态 /// </summary> public string State { get { return LabState.Text; } set { LabState.Text = value; Application.DoEvents(); } } private void TimerState_Tick(object sender, EventArgs e) { try { ServiceController scDBService = new ServiceController(serviceName); ServiceController[] scAllService = ServiceController.GetServices(); int i = scAllService.Length - 1; while (i > 0) { if (scAllService[i].DisplayName == serviceName) { if (scDBService.Status.Equals(ServiceControllerStatus.Running)) { State = serviceName + " 正在运行..."; } else if (scDBService.Status.Equals(ServiceControllerStatus.Stopped)) { State = serviceName + " 已经停止..."; } if (TimerState.Enabled) TimerState.Start(); return; } else { i--; } } State = serviceName + " 并没有安装在本机上,请检查。"; if (TimerState.Enabled) TimerState.Stop(); } catch (Exception exp) { State = serviceName + " 启动失败,原因:" + exp.Message; } } private void BtnExit_Click(object sender, EventArgs e) { Application.Exit(); } }} namespace EAssistantMonitorServiceManage{ partial class FrmMain { /// <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 Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain)); this.LabState = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.TimerState = new System.Windows.Forms.Timer(this.components); this.BtnExit = new System.Windows.Forms.Button(); this.BtnStopService = new System.Windows.Forms.Button(); this.BtnStartService = new System.Windows.Forms.Button(); this.SuspendLayout(); // // LabState // this.LabState.AutoSize = true; this.LabState.ForeColor = System.Drawing.Color.DarkRed; this.LabState.Location = new System.Drawing.Point(73, 53); this.LabState.Name = "LabState"; this.LabState.Size = new System.Drawing.Size(95, 12); this.LabState.TabIndex = 13; this.LabState.Text = "没有启动服务..."; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 53); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 12; this.label2.Text = "服务状态:"; // // TimerState // this.TimerState.Interval = 500; this.TimerState.Tick += new System.EventHandler(this.TimerState_Tick); // // BtnExit // this.BtnExit.Location = new System.Drawing.Point(174, 12); this.BtnExit.Name = "BtnExit"; this.BtnExit.Size = new System.Drawing.Size(75, 23); this.BtnExit.TabIndex = 11; this.BtnExit.Text = "退出"; this.BtnExit.UseVisualStyleBackColor = true; this.BtnExit.Click += new System.EventHandler(this.BtnExit_Click); // // BtnStopService // this.BtnStopService.Location = new System.Drawing.Point(93, 12); this.BtnStopService.Name = "BtnStopService"; this.BtnStopService.Size = new System.Drawing.Size(75, 23); this.BtnStopService.TabIndex = 10; this.BtnStopService.Text = "停止服务"; this.BtnStopService.UseVisualStyleBackColor = true; this.BtnStopService.Click += new System.EventHandler(this.BtnStopService_Click); // // BtnStartService // this.BtnStartService.Location = new System.Drawing.Point(12, 12); this.BtnStartService.Name = "BtnStartService"; this.BtnStartService.Size = new System.Drawing.Size(75, 23); this.BtnStartService.TabIndex = 9; this.BtnStartService.Text = "启动服务"; this.BtnStartService.UseVisualStyleBackColor = true; this.BtnStartService.Click += new System.EventHandler(this.BtnStartService_Click); // // FrmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(395, 80); this.Controls.Add(this.LabState); this.Controls.Add(this.label2); this.Controls.Add(this.BtnExit); this.Controls.Add(this.BtnStopService); this.Controls.Add(this.BtnStartService); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FrmMain"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "监控服务管理"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label LabState; private System.Windows.Forms.Label label2; private System.Windows.Forms.Timer TimerState; private System.Windows.Forms.Button BtnExit; private System.Windows.Forms.Button BtnStopService; private System.Windows.Forms.Button BtnStartService; }} 请单击 ServiceInstaller 组件并将 StartType 属性设置为适当的值。ü Manual 服务安装后,必须手动启动。ü Automatic 每次计算机重新启动时,服务都会自动启动。ü Disabled 服务无法启动。 vs2008到vs2005的语句转换 如何获取winform中正在运行的Timer计时器 悬赏:哪位老大知道QQ的聊天记录保存格式? 反射与引用DLL的区别 dotNet中怎行实现 BitBlt 功能 Tif文件转换问题 怎么使用非常规的C#.NET代码模拟WEB表单提交 请问代理IP,主要是针对浏览器有效吗? DateGrid绑定ComboBox出错(未将对象引用设置到对象的实例) 關於組件開發的菜鳥問題 .net开发管理员后台左侧导航的时候,用什么控件实现哈 有关读写Txt文件的问题
“net start 服务名”
即可开启PS:其实连服务安装都是可以通过DOS脚本来执行的,因此整个过程写个批处理比较好。至于批处理文件,你可以包含在项目中,也可以让程序在临时目录创建个临时的bat文件,写入脚本内容,之后保存调用执行。
启动服务
设置StartType属性(启动服务的方式,分为手动、自动和禁用)
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;namespace EAssistantMonitorServiceManage
{
public partial class FrmMain : Form
{
string serviceName = "监控服务名称";
public FrmMain()
{
InitializeComponent();
TimerState.Enabled = true;
} private void BtnStartService_Click(object sender, EventArgs e)
{
try
{
ServiceController scDBService = new ServiceController(serviceName);
ServiceController[] scAllService = ServiceController.GetServices();
int i = scAllService.Length - 1;
while (i > 0)
{
if (scAllService[i].DisplayName == serviceName)
{
if (scDBService.Status.Equals(ServiceControllerStatus.Stopped))
{
State = serviceName + " 正在启动...";
scDBService.Start();
}
else if (scDBService.Status.Equals(ServiceControllerStatus.Running))
{
State = serviceName + " 正在运行...";
}
if (!TimerState.Enabled) TimerState.Start();
return;
}
else
{
i--;
}
}
State = serviceName + " 并没有安装在本机上,请检查。";
if (TimerState.Enabled) TimerState.Stop();
}
catch (Exception exp)
{
State = serviceName + " 启动失败,原因:" + exp.Message;
}
} private void BtnStopService_Click(object sender, EventArgs e)
{
try
{
ServiceController scDBService = new ServiceController(serviceName);
ServiceController[] scAllService = ServiceController.GetServices();
int i = scAllService.Length - 1;
while (i > 0)
{
if (scAllService[i].DisplayName == serviceName)
{
if (scDBService.Status.Equals(ServiceControllerStatus.Running))
{
State = serviceName + " 正在停止...";
scDBService.Stop();
}
else if (scDBService.Status.Equals(ServiceControllerStatus.Stopped))
{
State = serviceName + " 已经停止...";
}
if (TimerState.Enabled) TimerState.Start();
return;
}
else
{
i--;
}
}
State = serviceName + " 并没有安装在本机上,请检查。";
if (TimerState.Enabled) TimerState.Stop();
}
catch (Exception exp)
{
State = serviceName + " 启动失败,原因:" + exp.Message;
}
} /// <summary>
/// 服务状态
/// </summary>
public string State
{
get { return LabState.Text; }
set
{
LabState.Text = value;
Application.DoEvents();
}
} private void TimerState_Tick(object sender, EventArgs e)
{
try
{
ServiceController scDBService = new ServiceController(serviceName);
ServiceController[] scAllService = ServiceController.GetServices();
int i = scAllService.Length - 1;
while (i > 0)
{
if (scAllService[i].DisplayName == serviceName)
{
if (scDBService.Status.Equals(ServiceControllerStatus.Running))
{
State = serviceName + " 正在运行...";
}
else if (scDBService.Status.Equals(ServiceControllerStatus.Stopped))
{
State = serviceName + " 已经停止...";
}
if (TimerState.Enabled) TimerState.Start();
return;
}
else
{
i--;
}
}
State = serviceName + " 并没有安装在本机上,请检查。";
if (TimerState.Enabled) TimerState.Stop();
}
catch (Exception exp)
{
State = serviceName + " 启动失败,原因:" + exp.Message;
}
} private void BtnExit_Click(object sender, EventArgs e)
{
Application.Exit();
} }
}
{
partial class FrmMain
{
/// <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 Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain));
this.LabState = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.TimerState = new System.Windows.Forms.Timer(this.components);
this.BtnExit = new System.Windows.Forms.Button();
this.BtnStopService = new System.Windows.Forms.Button();
this.BtnStartService = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// LabState
//
this.LabState.AutoSize = true;
this.LabState.ForeColor = System.Drawing.Color.DarkRed;
this.LabState.Location = new System.Drawing.Point(73, 53);
this.LabState.Name = "LabState";
this.LabState.Size = new System.Drawing.Size(95, 12);
this.LabState.TabIndex = 13;
this.LabState.Text = "没有启动服务...";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 53);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 12;
this.label2.Text = "服务状态:";
//
// TimerState
//
this.TimerState.Interval = 500;
this.TimerState.Tick += new System.EventHandler(this.TimerState_Tick);
//
// BtnExit
//
this.BtnExit.Location = new System.Drawing.Point(174, 12);
this.BtnExit.Name = "BtnExit";
this.BtnExit.Size = new System.Drawing.Size(75, 23);
this.BtnExit.TabIndex = 11;
this.BtnExit.Text = "退出";
this.BtnExit.UseVisualStyleBackColor = true;
this.BtnExit.Click += new System.EventHandler(this.BtnExit_Click);
//
// BtnStopService
//
this.BtnStopService.Location = new System.Drawing.Point(93, 12);
this.BtnStopService.Name = "BtnStopService";
this.BtnStopService.Size = new System.Drawing.Size(75, 23);
this.BtnStopService.TabIndex = 10;
this.BtnStopService.Text = "停止服务";
this.BtnStopService.UseVisualStyleBackColor = true;
this.BtnStopService.Click += new System.EventHandler(this.BtnStopService_Click);
//
// BtnStartService
//
this.BtnStartService.Location = new System.Drawing.Point(12, 12);
this.BtnStartService.Name = "BtnStartService";
this.BtnStartService.Size = new System.Drawing.Size(75, 23);
this.BtnStartService.TabIndex = 9;
this.BtnStartService.Text = "启动服务";
this.BtnStartService.UseVisualStyleBackColor = true;
this.BtnStartService.Click += new System.EventHandler(this.BtnStartService_Click);
//
// FrmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(395, 80);
this.Controls.Add(this.LabState);
this.Controls.Add(this.label2);
this.Controls.Add(this.BtnExit);
this.Controls.Add(this.BtnStopService);
this.Controls.Add(this.BtnStartService);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FrmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "监控服务管理";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Label LabState;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Timer TimerState;
private System.Windows.Forms.Button BtnExit;
private System.Windows.Forms.Button BtnStopService;
private System.Windows.Forms.Button BtnStartService;
}
}
ü Manual 服务安装后,必须手动启动。
ü Automatic 每次计算机重新启动时,服务都会自动启动。
ü Disabled 服务无法启动。