启动应用程序而不是看到控制台

时间:2012-01-17 10:47:47

标签: windows

我有一个应用程序(在.NET中),我想通过脚本在Windows操作系统上启动它,而不是看到它的控制台(将被隐藏)。
我怎么能这样做?

Spasiba

2 个答案:

答案 0 :(得分:1)

无论最终调用CreateProcess需要传递CREATE_NO_WINDOW进程创建标志。

  

该进程是一个在没有控制台窗口的情况下运行的控制台应用程序。因此,未设置应用程序的控制台句柄。

究竟如何最好地从脚本中实现这一点取决于您使用的脚本语言。

答案 1 :(得分:0)

您可以将其作为服务的一部分运行,但这需要它具有自安装代码或手动安装。

另一种方法是编写Windows窗体应用程序,但没有窗体并执行以下操作:

using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            string ApplicationPath = @"c:\consoleapplication.exe";

            // Create a new process object
            Process ProcessObj = new Process();

            // StartInfo contains the startup information of
            // the new process
            ProcessObj.StartInfo.FileName = ApplicationPath;



                // These two optional flags ensure that no DOS window
                // appears
                ProcessObj.StartInfo.UseShellExecute = false;
                ProcessObj.StartInfo.CreateNoWindow = true;

                // If this option is set the DOS window appears again :-/
                // ProcessObj.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                // This ensures that you get the output from the DOS application
                ProcessObj.StartInfo.RedirectStandardOutput = true;

                // Start the process
                ProcessObj.Start();

                // Wait that the process exits
                ProcessObj.WaitForExit();

            }
        }
    }

然后,您可以使用脚本调用此应用程序。