一般来说要开启一个程序是用Process.Start(),但最近我在服务程序启动时调用Process.Start()开启了一个窗口程序,但那个窗口程序在使用时总有问题。假如那个窗口程序不在服务里启动,而是手工启动,就没问题。所以现在我有些糊涂了。
我想问一下,除了Process.Start(),还有没有其他可以启动程序的方法?

解决方案 »

  1.   

    using System.Runtime.InteropServices;[DllImport("kernel32.dll")]
    internal static extern uint WinExec(string lpCmdLine, uint uCmdShow);
    private const uint SW_SHOW = 5;private void button1_Click(object sender, EventArgs e)
    {
        WinExec(@"c:\temp\temp.exe", SW_SHOW); 
    }
      

  2.   

    你的那个程序,如果是系统服务类型的,最好使用ServiceController 类 来控制.
      

  3.   

    下面的示例使用 ServiceController 类检查警报器服务是否已停止。如果该服务已停止,此示例将启动该服务并等待服务状态设置为 Running。// Check whether the Alerter service is started.ServiceController sc  = new ServiceController();
    sc.ServiceName = "Alerter";
    Console.WriteLine("The Alerter service status is currently set to {0}", 
                       sc.Status.ToString());if (sc.Status == ServiceControllerStatus.Stopped)
    {
       // Start the service if the current status is stopped.   Console.WriteLine("Starting the Alerter service...");
       try 
       {
          // Start the service, and wait until its status is "Running".
          sc.Start();
          sc.WaitForStatus(ServiceControllerStatus.Running);
       
          // Display the current service status.
          Console.WriteLine("The Alerter service status is now set to {0}.", 
                             sc.Status.ToString());
       }
       catch (InvalidOperationException)
       {
          Console.WriteLine("Could not start the Alerter service.");
       }
    }