在控制台中写的内容写到文件中

在控制台中写的内容写到文件中_第1张图片

Console.WriteLine("是否创建文本?Y/N");
            string Answer = Console.ReadLine();
            if (Answer.ToLower() == "y")
            {
                using (FileStream file = new FileStream("txt.txt", FileMode.Create, FileAccess.Write))
                {
                    while (true)
                    {
                        string lines = Console.ReadLine();
                        if (lines == "q")
                        {
                            Console.WriteLine("程序结束,请按任意键保存文本");
                            Console.ReadKey();
                            return;
                        }
                        byte[] bt = Encoding.Default.GetBytes(lines);
                        for (int i = 0; i < bt.Length; i++)
                        {
                            file.WriteByte(bt[i]);
                        }
                        file.WriteByte((byte)('\r'));
                        file.WriteByte((byte)('\n'));
                    }
                }
            }
            else if (Answer.ToLower() == "n")
            {
                Console.WriteLine("请输入任意键结束!");
                Console.ReadKey();
                return;
            }
            else
            {
                Console.WriteLine("输入有误,程序结束!");
                return;
            }

你可能感兴趣的:(小项目功能模块开发实例)