佑美a900还是w999好:C#可再windows上安装吗

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 10:24:51

上边的答复好像有点过头了吧。
好像只是问是否可以安装到Windows上而已!
答案是可以,只要是微软自己开发的东西,毫无疑问绝对可以安装到Windows上边。
只是Windows版本不同,可能需要附加安装一些东西!

两个字 可以

1、新建服务项目

比如

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Windows.Forms;
namespace CatchArticle
{
public class CatchArticle : System.ServiceProcess.ServiceBase
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
private Thread MainThread;
public CatchArticle()
{
// 该调用是 Windows.Forms 组件设计器所必需的。
InitializeComponent();

MainThread=new Thread(new ThreadStart(ThreadFunc));
MainThread.Priority=ThreadPriority.Lowest;
}

// 进程的主入口点
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CatchArticle() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

///
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
///
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "CatchArticle";
}

///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

///
/// 设置具体的操作,以便服务可以执行它的工作。
///
protected override void OnStart(string[] args)
{
MainThread.Start();
}

///
/// 停止此服务。
///
protected override void OnStop()
{
MainThread.Abort();
}

public static void ThreadFunc()
{
int LastHour=DateTime.Now.Hour;
while (true)
{
System.Threading.Thread.Sleep(1000);
if (DateTime.Now.Hour-1==LastHour)
{
MessageBox.Show("为了爱护您的眼睛,请您暂时休息5分钟并向远处眺望!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);
LastHour=DateTime.Now.Hour;
}
}
}

}
}

========================================

2、然后增加一个ProjectInstall.cs 继承自Installer ,里面的一些参数可以自己定义

using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;

namespace CatchArticle
{
///
/// myInstall 的摘要说明。
///
///
[RunInstaller(true)]
public class myInstall : Installer
{

private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public myInstall()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();

processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "CatchArticle";

Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}

3、然后便以Release

4、使用C:\......\Framework\v1.1..\installUtil.exe C:\项目路径\bin\release\TestService.exe

注意:如果不增加ProjectInstall.cs,则会报错