将windows服务在远程计算机上运行

来源:互联网 发布:领导力网络课测验答案 编辑:程序博客网 时间:2024/05/29 07:47

最近写了一个小程序,在远程服务器上运行,可是在系统注销当前登陆用户时,用户启动的程序也停止了,查了很多资料,也尝试修改注册表等,都不理想,干脆不偷懒了,老老实实写成windows服务吧;建立一个简单的windows服务的步骤如下:

(1).新建一个windows服务工程,在设计页面上点右键,出现菜单后,选择添加安装程序。这时会出现一个新的页面,页面上有个控件 serviceProcessInstaller1和serviceInstaller1,在 serviceProcessInstaller1中把属性Account改为LocalSystem,再把serviceInstaller1中把属性Parent 改为serviceProcessInstaller1  ,ServiceName属性是生成服务后的服务名字(假如为Msg).

(2).把这个控件的属性改完以后。回到新建的服务页的后台,添加自己的代码:

(3).代码书写完成后,生成一下,找到工程目录下刚生成的exe文件,把这个文件复制到f盘.在运行输入cmd,进入以下目录:cd C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727,安装此服务:installutil f:/ServiceTest.exe.(卸载服务的方法为:installutil f:/ServiceTest.exe-u).

(4).服务安装完成后,启动服务 net start msg,(msg为服务名称),这样一个简单的服务就完成了.附代码:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace MsgService
{
    
/// <summary>
    
/// Explain: Windows服务
    
/// Author:YMQ
    
/// CreateDate:2008-04-02
    
/// Last Modify:
    
/// </summary>

    public partial class Service1 : ServiceBase
    
{
       
        
private System.Timers.Timer timerSender;

        
public Service1()
        
{
            InitializeComponent();
            
this.timerSender = new System.Timers.Timer();
            
this.timerSender.Interval = 120000;  //2分钟
            this.timerSender.Elapsed += new System.Timers.ElapsedEventHandler(timerSender_Elapsed);
        }


        
//服务启动        
       protected override void OnStart(string[] args)
        
{
            
try
            
{
                 
string strPath = AppDomain.CurrentDomain.BaseDirectory;  //应用程序相对路径
                 StreamWriter sw = new StreamWriter(@strPath + @"SendMsgLog.txt"true, System.Text.Encoding.Default);
                sw.Write(DateTime.Now.ToShortDateString() 
+ " " + DateTime.Now.ToShortTimeString() + " ");
                sw.Close();

              
this.timerSender.Enabled = true;  //启动循环发送
            }

            
catch (Exception ex)
            
{
                
string strPathErr = AppDomain.CurrentDomain.BaseDirectory;
                StreamWriter swerr 
= new StreamWriter(@strPathErr + @"SendMsgErr.txt"true, System.Text.Encoding.Default);
                swerr.Write(ex.Message
+" ");
                swerr.Close();
            }

        }


        
protected override void OnStop()
        
{
            
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }


        
//循环检测发送信息
        private void timerSender_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        
{
            StreamWriter sw
=null;
            
try
            
{
                
string strPath = AppDomain.CurrentDomain.BaseDirectory;  //应用程序相对路径
                sw = new StreamWriter(@strPath + @"SendMsgLog.txt"true, System.Text.Encoding.Default);
               sw.Write(DateTime.Now.ToShortDateString() 
+ " " + DateTime.Now.ToShortTimeString() + " 启动: ");
             }

            
catch (Exception ex)
            
{
                
string strPathErr = AppDomain.CurrentDomain.BaseDirectory;  //应用程序相对路径
                StreamWriter swErr = new StreamWriter(@strPathErr + @"SendMsgErr.txt");
                swErr.Write(ex.Message
+" ");
                swErr.Close();
            }

            
finally
            
{
                sw.Close();
            }

        }

    }

}

 

 



 

原创粉丝点击