Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:23:56
C#教程:客户机调用Windows服务

C#教程:客户机调用Windows服务

客户机调用Windows服务

系统提供了一个组件serviceController来控制Windows服务,该组件的具体使用方法可参见Windows 控件与组件。

示例

使用serviceControlle组件控制服务

下面通过一个示例介绍如何启动和停止Windows服务。示例运行结果如图1和图2所示。



图1 启动服务



图2 停止服务

程序开发步骤如下所示。

(1)新建一个Windows窗体,并将其主窗体命名为“frmWindows”。

(2)在主窗体中添加一个serviceController组件,设置其属性,如图3所示。



图3 serviceController属性设置

窗体加载时,在窗体上显示服务名称、服务类别和计算机名。

(3)程序代码。

private void frmWindows_Load_1(object sender, EventArgs e)

{

this.textBox1.Text = this.serviceController1.ServiceName;

this.textBox2.Text = this.serviceController1.ServiceType.ToString();

this.textBox4.Text = this.serviceController1.MachineName;

ServiceControllerStatus star = this.serviceController1.Status;

switch (star)

{

case ServiceControllerStatus.Stopped:

this.bntStop.Enabled = False;

break;

case ServiceControllerStatus.Running:

this.bntStar.Enabled = false;

break;

}

}

最后,到D盘根目录下打开FileServer.txt文件查看效果。

完整程序代码如下:

★ ★★★★frmWindows.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.ServiceProcess;

namespace _3_01

{

public partial class frmWindows : Form

{

public frmWindows()

{

InitializeComponent();

}

private void bntStar_Click(object sender, EventArgs e)

{

this.serviceController1.Start();

MessageBox.Show("服务己启动", "服务提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

this.bntStar.Enabled = false;

this.bntStop.Enabled = true;

}

private void bntStop_Click(object sender, EventArgs e)

{

this.serviceController1.Stop();

MessageBox.Show("服务已停止", "服务提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

this.bntStop.Enabled = false;

this.bntStar.Enabled = true;

}

private void frmWindows_Load_1(object sender, EventArgs e)

{

this.textBox1.Text = this.serviceController1.ServiceName;

this.textBox2.Text = this.serviceController1.ServiceType.ToString();

this.textBox4.Text = this.serviceController1.MachineName;

ServiceControllerStatus star = this.serviceController1.Status;

switch (star)

{

case ServiceControllerStatus.Stopped:

this.bntStop.Enabled = false;

break;

case ServiceControllerStatus.Running:

this.bntStar.Enabled = false;

break;

}

}

}

}

★ ★★★★frmWindows.designer.cs窗体设计文件完整程序代码★★★★★

namespace _3_01

{

partial class frmWindows

{

///



/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759