现在的位置: 首页 > 综合 > 正文

在客户机上安装Windows服务

2011年03月31日 ⁄ 综合 ⁄ 共 7480字 ⁄ 字号 评论关闭
//目前此方法只在WindowsXP测试通过,在Windows2003服务器上不能应用
//本文主要介绍对用c#.net生成的Windows服务程序进行安装,共采用了两种方法,其中一种方法为调用
//installutil.exe进行安装,同时生成了 Process 类 的使用实例
//writer:furenjun 2006.4.28
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;



using System.Diagnostics;
using System.Reflection;
using System.Collections.Specialized;
using System.Text;

namespace installWindowsService
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
Private Variables Private Variables
        
DLLImport

        
private System.Windows.Forms.Button button1;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


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

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void button1_Click(object sender, System.EventArgs e)
        
{
            

            
string svcPath;
            
string svcName;
            
string svcDispName;
            
//服务程序的路径
            svcPath = @"E:\TransactionServiceCS\bin\Release\TransactionServiceCS.exe";
        
            
//OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory 
= "c:\\" ;
            openFileDialog1.Filter 
= "exe file(*.exe)|*.*" ;
            openFileDialog1.FilterIndex 
= 0 ;
            openFileDialog1.RestoreDirectory 
= true ;



            
if(openFileDialog1.ShowDialog() != DialogResult.OK)
            
{
            
                
return;
            }

            
else
                svcPath
=this.openFileDialog1.FileName;  
            svcDispName
="MyDBWriter";
            svcName
= "MyDBWriter";
            
            
if(InstallService(svcPath, svcName, svcDispName))
                MessageBox.Show(
"安装成功") ;
            Console.Read();

        }

        
/// <summary>
        
/// 安装和运行
        
/// </summary>
        
/// <param name="svcPath">程序路径.</param>
        
/// <param name="svcName">服务名</param>
        
/// <param name="svcDispName">服务显示名称.</param>
        
/// <returns>服务安装是否成功.</returns>

抱歉!评论已关闭.