C# 通过c在当前命令中显示消息#

C# 通过c在当前命令中显示消息#,c#,console,cmd,C#,Console,Cmd,我已经编写了在控制台和用户界面中工作的应用程序。现在,当从控制台执行应用程序时,我想在当前打开的控制台中显示消息 static class Program { [DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingCo

我已经编写了在控制台和用户界面中工作的应用程序。现在,当从控制台执行应用程序时,我想在当前打开的控制台中显示消息

static class Program
    {
        [DllImport("kernel32.dll",
            EntryPoint = "GetStdHandle",
            SetLastError = true,
            CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr GetStdHandle(int nStdHandle);
        [DllImport("kernel32.dll",
            EntryPoint = "AllocConsole",
            SetLastError = true,
            CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.StdCall)]
        private static extern int AllocConsole();
        private const int STD_OUTPUT_HANDLE = -11;
        private const int MY_CODE_PAGE = 437; 

        [STAThread]
        static void Main(string[] Args)
        {
                if (Args[0] != "")
                {
                    //AllocConsole();
                    commandlineTool(Args[0]);
                }
                else
                {

                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form2());
                }
static void commandlineTool(string filename)
{
    //all coding here
    AllocConsole();

    IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
    FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
    System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
    StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
    standardOutput.AutoFlush = true;
    Console.SetOut(standardOutput);

    Console.WriteLine("File has been generated.");
    Console.ReadLine();
}
实际上alloconsole()为流程分配新控制台

但是从这里会打开一个新控制台并显示消息,但我想在同一控制台中显示消息。

方法a(首选,简单): 您可以将该应用程序设置为控制台应用程序,并在GUI模式下启动时使用释放控制台。这是最好的解决办法。唯一的缺点是,启动应用程序时控制台窗口将显示得非常短暂

方法B(必要时使用): 您可以尝试使用连接到父进程的控制台。如果父进程没有控制台,这将失败,在这种情况下,您必须返回使用AllocConsole

另外,如果在尝试连接/分配控制台时类已经初始化,则必须重新连接输入/输出/错误流,如下所示:

    StreamWriter stdOut = new StreamWriter(Console.OpenStandardOutput());
    stdOut.AutoFlush = true;
    Console.SetOut(stdOut);

    StreamWriter stdErr = new StreamWriter(Console.OpenStandardError());
    stdErr.AutoFlush = true;
    Console.SetError(stdErr);

    StreamReader stdIn = new StreamReader(Console.OpenStandardInput());
    Console.SetIn(stdIn);
否则您将没有输出。

方法A(首选,简单): 您可以将该应用程序设置为控制台应用程序,并在GUI模式下启动时使用释放控制台。这是最好的解决办法。唯一的缺点是,启动应用程序时控制台窗口将显示得非常短暂

方法B(必要时使用): 您可以尝试使用连接到父进程的控制台。如果父进程没有控制台,这将失败,在这种情况下,您必须返回使用AllocConsole

另外,如果在尝试连接/分配控制台时类已经初始化,则必须重新连接输入/输出/错误流,如下所示:

    StreamWriter stdOut = new StreamWriter(Console.OpenStandardOutput());
    stdOut.AutoFlush = true;
    Console.SetOut(stdOut);

    StreamWriter stdErr = new StreamWriter(Console.OpenStandardError());
    stdErr.AutoFlush = true;
    Console.SetError(stdErr);

    StreamReader stdIn = new StreamReader(Console.OpenStandardInput());
    Console.SetIn(stdIn);
否则您将没有输出