All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

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

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:23:56 Browse times: 345 Comment times: 0

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


Head photo

Go homepage
Upload pictures
Write articles

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();




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.