我用VS2005(C#)写了一个Windows服务程序和另外一个窗体程序,现在要将两个程序合在一起做成一个安装包,请问如何在制作安装文件的时候能自动注册windows服务程序?在网上查了很多资料,都是在单个windows服务程序的解决方案里面再添加一个安装程序,但是我是有多个分开的项目,要单独建立一个安装项目,不知道怎么操作,望高手指点!

解决方案 »

  1.   

    要用到安装服务的InstallUtil.exe工具。打包的时候加一个BAT文件,里面写上安装服务的命令:
    InstallUtil -i 服务程序集文件名
    在安装的时候调用这个批处理文件就行了。
      

  2.   

    请问如何在安装的时候调用或者运行这个bat文件?另外,有没有通过VS2005直接设置就可以达到安装windows服务的办法?
      

  3.   

    就是在安装的时候执行一个函数,里面调用命令行,然后用
    Process.Start(@"C:\bat.BAT");
    这个会显示命令行窗口。如果楼主不想显示,可以不用BAT文件,直接在代码中调用CMD。参考这个Process   p   =   new   Process();  
                    p.StartInfo.FileName   =   "cmd.exe";  
                    p.StartInfo.UseShellExecute   =   false;  
                    p.StartInfo.RedirectStandardInput   =   true;  
                    p.StartInfo.RedirectStandardOutput   =   true;  
                    p.StartInfo.RedirectStandardError   =   true;  
                    p.StartInfo.CreateNoWindow   =   true;  //不显示命令行窗口
                    p.Start();  
                    p.StandardInput.WriteLine("InstallUtil -i 服务程序集文件名"); //确保InstallUtil在当前目录下  
      

  4.   

    都是在单个windows服务程序的解决方案里面再添加一个安装程序
    楼主说的是安装服务的程序,这个是添加Windows服务必须的。就是说楼主要在服务的解决方案中添加一个安装程序,代码怎么写楼主可以搜索一下,网上资料很多。
    然后把所有项目打包安装,这方面楼主可以参考
    C#程序打包与部署
    c#程序打包,同时把netframework也打包进去
    关于C#程序打包问题!
    在这个安装过程中调用cmd安装服务。
      

  5.   

    我也遇到过楼主这样的问题,自已先做一个安装服务启动程序,程序如下 
    static int Main(string[] args)
            {
                if (args.Length > 0)
                { 
                    System.Diagnostics.Process process = new Process();
                                    if (System.IO.File.Exists(Application.StartupPath + @"\InstallUtil.exe"))
                    {
                        process.StartInfo.FileName = Application.StartupPath + @"\InstallUtil.exe";
                        if (args.Length > 1 && System.IO.File.Exists(Application.StartupPath + @"\" + args[1]))
                            try
                            {
                                System.IO.FileStream _file = System.IO.File.OpenWrite(Application.StartupPath + @"\" + args[1]);
                                _file.Close();
                            }
                            catch
                            {
                                
                                return -1;
                            }
                        try
                        {
                            process.StartInfo.Arguments = (args.Length > 1 ? args[0] + " " + '"' + Application.StartupPath + @"\" + args[1] + '"' : '"' + Application.StartupPath + @"\" + args[0] + '"');
                                                  process.Start();
                        }
                        catch
                        { }
                    }
                    else
                    {
                        MessageBox.Show(Application.StartupPath + "  " + args[0]);
                    }            }
                System.Threading.Thread.Sleep(2000);
                return 0;
            }
    将InstallUtil.exe以及上面生成的可执行软件也打包到安装包里 然后在安装的自定义操作里,在提交里面增加生成的程序,并将 arguments 设置为你的服务程序(例server.exe) 同时将installerclass设置为false
    这样在安装时就会自动注册服务了
      

  6.   

    学习 还可以用这个 AssemblyInstaller Installer = new AssemblyInstaller();
                            IDictionary saveState = new Hashtable();
                            Installer.UseNewContext = true;
                            Installer.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "服务.exe");
                            saveState.Clear();
                            Installer.Install(saveState);
                            Installer.Commit(saveState);
                            Installer.Dispose();安装服务
      

  7.   

    参考
    http://topic.csdn.net/u/20071122/14/79750198-12a2-4fbb-88c2-aebcf10d93af.html
    http://blog.csdn.net/selfxd/archive/2007/07/17/1694808.aspx