读取控制台字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleUtilities
{
    class AcceptMutilLine
    {
        static string acceptmultiLine()
        {
            ConsoleKeyInfo cki;
            Console.TreatControlCAsInput = true;//防止Ctrl+C复制
            Console.WriteLine("Press the F10 key to quit: \n");
            string result = string.Empty;
            do
            {
                cki = Console.ReadKey();
                result += cki.KeyChar;
                if (cki.Key == ConsoleKey.Enter)
                {
                    result += System.Environment.NewLine;//如果输入回车,则加入换行标志
                }
            } while (cki.Key != ConsoleKey.F10);//按F10退出
            return result;
        }

        public static void Func(string[] args)
        {
            string s = acceptmultiLineChars2();
            Console.WriteLine(s);
            Console.ReadLine();
        }

        static string acceptmultiLineChars2()
        {
            ConsoleKeyInfo cki;
            Console.TreatControlCAsInput = true;//防止Ctrl+C复制
            Console.WriteLine("Press the CTRL+Enter key to quit: \n");
            string result = string.Empty;
            do
            {
                cki = Console.ReadKey();
                if (cki.Key == ConsoleKey.Enter)
                {
                    result += System.Environment.NewLine;//如果输入回车,则加入换行标志
                    Console.SetCursorPosition(0, Console.CursorTop + 1);//光标下移一行
                }
                result += cki.KeyChar;
            } while (cki.Key != ConsoleKey.Enter || (cki.Modifiers & ConsoleModifiers.Control) == 0);//按Ctrl+Enter退出
            return result;
        }
    }
}

posted @ 2012-03-21 16:56  沙耶  阅读(536)  评论(0编辑  收藏  举报