我已经做好了一个winfrom的程序,想要做的开机自动启动。需要做一个服务开完成,就是开启服务后期的我的winform程序。当然也可以停止。另外我的winform程序也可以暂停和启用我的服务程序。
两方面都不是很难,只不过没有做过服务不知道怎么下手。
有例子的发我邮箱,最好了,[email protected].

解决方案 »

  1.   

    http://zzk.cnblogs.com/so.aspx?w=+%E6%9C%8D%E5%8A%A1%E4%BA%A4%E4%BA%92&t=
      

  2.   

    大家帮忙看看,我也baidu了点例子,不过说的不是很清楚啊!
      

  3.   

    Winform中:
    System.ServiceProcess.ServiceController   serviceController   =   new   System.ServiceProcess.ServiceController(); 
    serviceController.ServiceName   =   ""; 
    serviceController.Start();//启动 
    serviceController.Stop();//停止服务中:  protected override void OnStart(string[] args)
            {
                // TODO: 在此处添加代码以启动服务。
                 timer1.Interval = 10000;//10秒检测一次
                timer1.Enabled = true;
            }
    private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
              Process[]   ps=Process.GetProcessesByName( "winform1"); 
              if(ps.Length==0) 
                  {
                    Process.Start( "winform1.exe ");
                }
            }服务的StartType属性改成Automatic,自动启动
      

  4.   

    你好,我还想问一下,我怎么才能获取到这个服务的跟目录呢?就是服务exe的目录?
      

  5.   


    是的,服务的目录就是那个。
    发布服务的方法有几种,我常用的就是 切换到服务的视图界面,右击 添加安装程序,多出来一个ProjectInstaller.cs, 然后在视图界面上把serviceInstaller1的Desription(服务的描述改下),
    serviceProcessInstaller1的Account改成LocalSystem。。然后重新生成。cmd命令:
    cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727InstallUtil -u (拆卸服务)
    E:\yn_sound\ServerForMoveFile\ServerForMoveFile\ServerForMoveFile\bin\Debug\ServerForMoveFile.exe
    InstallUtil    (安装服务)E:\yn_sound\ServerForMoveFile\ServerForMoveFile\ServerForMoveFile\bin\Debug\ServerForMoveFile.exe
      

  6.   

    你还,我还有个问题,就是启动我的服务后,服务会调用一个exe程序,出现个“启动此实时调试器时未启用必须的安全权限,要调试该进程,必须以管理员身份运行此实时调试器” 我要怎么处理?
      

  7.   

    不好意思,这个问题我没有遇见过。。(调试服务是要点点击VS工具栏上 调试。附加到进程才行。)
    如果是权限问题的话,serviceProcessInstaller1的Account改成LocalSystem就是把服务的运行账户改成本地系统。。你试试改下 服务的运行账户 权限。应该就能解决
      

  8.   

    因为可能你 服务运行的权限是User,如果你exe运行需要的LocalSystem,这里面可能就有冲突,个人猜测。
      

  9.   

    我看有修改权限的
    app.manifest修改如下:
       <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
       <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
       </requestedPrivileges>
    不过,我修改之后 出现了 ClickOnce 不支持请求执行级别“requireAdministrator”
    的错误。
      

  10.   

    我已经修改成LocalSystem还是出现这个问题,我的系统是Windows 7 的
      

  11.   


    win7的权限控制是挺烦的。我了解不多。。win7好像可以把exe改成始终用管理员身份运行吧。
    你参考下这个。http://www.cnblogs.com/likewind/archive/2010/10/20/1856604.html
      

  12.   

    本质上,你需要做一个服务installer和一个服务控制器。
    前一个很简单,在服务的[设计]界面上,右击空白处,选“添加安装程序”。后面自己搞吧,很简单。
    服务控制器:class CServiceController     {
            private ServiceController m_watcherSC = null;
                    public bool start() 
            {
                if (m_watcherSC.Status == ServiceControllerStatus.Stopped)
                {
                    // Start the service, and wait until its status is "Running".
                    m_watcherSC.Start();
                    m_watcherSC.WaitForStatus(ServiceControllerStatus.Running);
                }
                return true;
            }        public bool stop()
            {
                if (m_watcherSC.Status == ServiceControllerStatus.Running)
                {
                    // Start the service, and wait until its status is "Stopped".
                    m_watcherSC.Stop();
                    m_watcherSC.WaitForStatus(ServiceControllerStatus.Stopped);
                }
                return true;
            }        public void setControlleeName(string name)
            {
                m_watcherSC = new ServiceController(name);
            }        public string getControlleeStatus()
            {
                return getServiceStatus(m_watcherSC);
            }
            /// <summary>
            /// 获取服务的状态,以中文形式输出。
            /// </summary>
            /// <param name="sc">服务</param>
            /// <returns>服务状态</returns>
            private string getServiceStatus(ServiceController sc)
            {
                string status = "";
                switch (sc.Status)
                {
                    case ServiceControllerStatus.Running:
                        status = sc.ServiceName + "服务已运行。";
                        break;
                    case ServiceControllerStatus.Stopped:
                        status = sc.ServiceName + "服务已停止。";
                        break;
                    case ServiceControllerStatus.StopPending:
                        status = sc.ServiceName + "服务正在停止。";
                        break;
                    case ServiceControllerStatus.Paused:
                        status = sc.ServiceName + "服务已暂停。";
                        break;
                    case ServiceControllerStatus.StartPending:
                        status = sc.ServiceName + "服务正在启动。";
                        break;
                }
                return status;
            }
        }
      

  13.   

    我想你可能没有明白我的意思。我现在可以实现服务的启动或者运行什么的,我现在的问题是,我在onstart中想要调用我的exe程序,在这个地方出现了,“启动此实时调试器时未启用必须的安全权限,要调试该进程,必须以管理员身份运行此实时调试器” 的问题。
      

  14.   

    好吧,还是没有解决问题,不过把分给好心的“ooo7880000”吧