All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
SQL,Server连接基础知识的详细说明_[SQL,Server教程]
.net获取w3wp进程对应的应用程序池_[Asp.Net教程]
使用php的zlib压缩和解压缩swf文件_[PHP教程]
windows下Apache2+php4+MySql5集成简明教程_[PHP教程]
ASP.NET中的日期与时间的处理方法_.net资料_编程技术
PHP安全配置之实现安全的两个重点_php资料_编程技术
ClassWizard的功能
HTML语言:什么是Unicode?什么是UTF-8?_[Html教程]
全面接触SQL语法(4)_[SQL,Server教程]
PHP入门:动态网页制作技术PHP的变量类型_[PHP教程]
Reading number is top 10 pictures
The real super beauty13
生活中总有些低调的人,不经意间散发出土豪的气质
The wise woman of chest2
Wear bikinis cock
两张抽象画
鸡也看毛片
来几张有特色的图片
自己约的炮,含泪也要打完
这玉米,买还是不卖?
A beautiful girl to bud2
Download software ranking
Boxer's Top ten classic battle10
Proficient in JavaScript
Sora aoi 120 minutes
Love the forty days
C语言教程TXT
塘西风月痕
Unix video tutorial12
Unix video tutorial15
都市狐狸姑娘传
asp.netWeb服务器高级编程
qq published in(发表于) 2014/7/11 9:23:56 Edit(编辑)
C#教程:客户机调用Windows服务

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


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.