C# Mono Console.ReadLine 退出

标签 c# linux ubuntu mono

我正在 linux mint 14 上使用 MONO (C#) 构建应用程序,但我遇到了一些问题。我在选项中启用了外部控制台,在调试时它工作得很好但是在部署之后(在调试文件夹中打开 .exe)应用程序在 Console.ReadLine(); 之后立即退出;有什么想法吗?

public static void InitializeUI() 
        {
            Console.WriteLine("On the bottom line enter the command \"START\" followed by a space and a number representing on how many hours to check for new posts. You can always stop the execution with Ctrl+C \r\n \r\nExample : START 6");
            ParseCMD();
            Console.ReadLine ();
        }
        private static void ParseCMD()
        {
            string cmd = Console.ReadLine();
            string[] commands = cmd.Trim().Split(new string[] {" "},StringSplitOptions.RemoveEmptyEntries);
            int delay = 0;
            if (commands[0].ToLower() == "start" && int.TryParse(commands[1],out delay))
            {
                MainLogic.StartLogic(delay);
            }
            else
            {
                Console.WriteLine("Wrong command");
                ParseCMD();
            }
        }

之后退出
string cmd = Console.ReadLine();

最佳答案

    public static void Main(string[] args)
    {
        Console.WriteLine("On the bottom line enter the command \"START\" followed by a space and a number representing on how many hours to check for new posts. You can always stop the execution with Ctrl+C \r\n \r\nExample : START 6");


        if(!ParseCMD(readKeey()))
        {
            ParseCMD(readKeey());
        }

    }private static string readKeey()
    {
        ConsoleKeyInfo cki;
        Console.TreatControlCAsInput = true;

        string temp="";
        do
        {
            cki = Console.ReadKey();
            temp=temp+cki.KeyChar;

        } while (cki.Key != ConsoleKey.Enter);
        return temp;
    }

    private static bool ParseCMD(string text)
    {
        try{
            string cmd = text;
            string[] commands = cmd.Trim().Split(new string[] {" "},StringSplitOptions.RemoveEmptyEntries);
            int delay = 0;
            if (commands[0].ToLower() == "start" && int.TryParse(commands[1],out delay))
            {
                Console.WriteLine("Right command you enter:" + commands[0] + commands[1]);
                return true;
            }
            else
            {
                Console.WriteLine("Wrong command");

                return false;
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine("ex.Message:"+ex.Message);
            return false;
        }
    }
}}

我想这与 Mono 无关,请您尝试使用 Console.ReadKey() 代替。

关于C# Mono Console.ReadLine 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16128909/

相关文章:

存储在 shell 变量中的正则表达式在双括号之间不起作用

Pythonsetuptools pkg_resources pip wheel 失败,错误代码为 1 [错误]

PHP 使用 LetsEncrypt 验证失败

firefox - 如何运行两个独立的 Firefox 实例?

c# - Application Insights - 异常添加信息

linux - 从 PCAP 中提取信息

java - 将 Java 编译器转换为 Linux

C#,在List<>.ConvertAll方法中使用lambda表达式,当对象为null时如何进行过滤?

c# - 无法导航到 RowSelect 上的 ViewController

c# - 如何删除在 DataDirectory 中创建的 .sdf 文件