混合控制台/表单应用程序始终打开第二个控制台

时间:2019-04-01 15:34:33

标签: c# winforms console

我正在编写一个主要用于控制台应用程序的应用程序,但是其中一些功能会打开/使用表格来完成其工作。

一切正常,除了被调用时,应用程序总是会打开一个附加的控制台窗口,在该窗口中执行所有控制台操作和交互(此多余的第二个窗口随后在程序中关闭终止)。这是完全有害的行为,但是我不知道为什么会产生第二个窗口,也不知道应用程序为什么坚持只从那个窗口进行写入/读取,而不是一个线索它被称为...

这是我的Program.cs文件的内容...

using System;

namespace CodeLibrary.ConsoleFunctions
{
    /// <summary>Simple static routines to facilitate writing text to the Console in color more efficiently.</summary>
    public static class Con
    {

        #region Methods
        /// <summary>Output specified text to the Console at the current cursor position, in the specified colours.</summary>
        /// <param name="data">The text to write.</param>
        /// <param name="fore">The foreground colour to use (default = LightGray).</param>
        /// <param name="back">The background colour to use (default = black).</param>
        public static void Write(string data, ConsoleColor fore, ConsoleColor back = ConsoleColor.Black)
        {
            CliColor store = CliColor.CaptureConsole();

            Console.ForegroundColor = fore;
            Console.BackgroundColor = back;
            Console.Write(data);

            store.ToConsole();
        }

        public static void Write(string data, CliColor color = null) =>
            Write(data, ((color is null) ? CliColor.Default.Fore : color.Fore), ((color is null) ? CliColor.Default.Back : color.Back));

        /// <summary>Output specified text to the Console at the current cursor position, in the specified colours,followed by a newline.</summary>
        /// <param name="data">The text to write.</param>
        /// <param name="fore">The foreground colour to use (default = LightGray).</param>
        /// <param name="back">The background colour to use (default = black).</param>
        public static void WriteLn(string data, ConsoleColor fore = ConsoleColor.Gray, ConsoleColor back = ConsoleColor.Black)
        {
            Write(data, fore, back);
            Console.WriteLine();
        }

        /// <summary>Sends text to the Console with a section highlighted in a different colour.</summary>
        /// <param name="first">The first section of text to write in the basic foreground colour.</param>
        /// <param name="second">The second section of text to write in the highlight colour.</param>
        /// <param name="third">An optional third section of text to write, back in the basic foreground colour.</param>
        /// <param name="fore">The foreground colour to use for the first and third strings (default = LightGray).</param>
        /// <param name="highlight">The foreground colour to use for the second string (default = White)</param>
        /// <param name="back">The background colour to use throughout (default = black).</param>
        public static void Highlight(string first, string second, string third = "", ConsoleColor fore = ConsoleColor.Gray, ConsoleColor highlight = ConsoleColor.White, ConsoleColor back = ConsoleColor.Black)
        {
            Write(first, fore, back);
            Write(second, highlight, back);
            Write(third, fore, back);
        }

        /// <summary>Sends text to the Console with a section highlighted in a different colour and followed with a newline.</summary>
        /// <param name="first">The first section of text to write in the basic foreground colour.</param>
        /// <param name="second">The second section of text to write in the highlight colour.</param>
        /// <param name="third">An optional third section of text to write, back in the basic foreground colour.</param>
        /// <param name="fore">The foreground colour to use for the first and third strings (default = LightGray).</param>
        /// <param name="highlight">The foreground colour to use for the second string (default = White)</param>
        /// <param name="back">The background colour to use throughout (default = black).</param>
        public static void HighlightLn(string first, string second, string third = "", ConsoleColor fore = ConsoleColor.Gray, ConsoleColor highlight = ConsoleColor.White, ConsoleColor back = ConsoleColor.Black) =>
            Highlight(first, second, third + "\r\n", fore, highlight, back);

        /// <summary>Implements a basic "Press Any Key" pause.</summary>
        /// <param name="withEsc">If set to TRUE, adds the text "(or [ESC] to quit)." to the prompt.</param>
        /// <param name="fore">The foreground colour to apply to the base text.</param>
        /// <param name="back">The background colour to use.</param>
        /// <param name="highlight">Optional highlight colour to apply to the "[ESC]" portion of the output.</param>
        /// <returns></returns>
        public static char WaitPrompt(int indent = 0, bool withEsc = false, ConsoleColor fore = ConsoleColor.Gray, ConsoleColor back = ConsoleColor.Black, ConsoleColor highlight = ConsoleColor.White)
        {
            Write("Press any key...".PadLeft(indent,' '), fore, back);
            if (withEsc) Highlight("(or ", "[ESC]", " to quit).", fore, highlight, back);
            char result = Console.ReadKey().KeyChar;

            // Using \r's, without a \n causes the Cursor to return to column 1, of the same line. This effectively clears
            // the prompt and leaves the cursor where it would have been if the prompt hadn't come up.
            Write("\r                                      \r", fore, back);
            return result;
        }
        #endregion
    }
}

尽管我认为这无关紧要,但此处使用的“ Con”接口只是一个中间类,用于使Console编写更加实用/友好的内容:

// prettier-ignore

我要让应用程序在调用它的控制台窗口的范围内工作,如何使它停止创建/访问它的第二个实例?

1 个答案:

答案 0 :(得分:0)

我在重新创建问题时遇到问题,但请检查您的应用程序是否设置为ConsoleApplication 在解决方案资源管理器中右键单击该项目。转到“属性”,将您的输出类型设置为“控制台应用程序”。 Screenshot