请参阅以下计划:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FileOperation1
{
class FileMain
{
static void Main(string[] args)
{
FileMain fm = new FileMain();
char ch = fm.Menu();
while (ch != '0')
{
switch (ch)
{
case '0':
break;
case '1':
//Console.WriteLine("This featute is not implemented till now.");
break;
case '2':
Console.Write("Enter the name of the file: ");
String FileName = Console.ReadLine();// ReadLine() method is not workin here
FileOperation Fo=new FileOperation();
Console.WriteLine("\n" + Fo.FileRead(FileName, FileMode.Open, FileAccess.Read));
break;
case '3':
//Console.WriteLine("This featute is not implemented till now.");
break;
default:
Console.WriteLine("Invalid choice. Enter again.");
break;
}
ch = fm.Menu();
}
}
private char Menu()
{
Console.WriteLine("\n\t***File Operations***");
Console.WriteLine("1. Create a new file");
Console.WriteLine("2. Open a file");
Console.WriteLine("3. Edit an existing file");
Console.WriteLine("0. Exit");
Console.Write("\nEnter your choice: ");
char ch = Convert.ToChar(Console.Read()); //Read() Method is not working properly
return ch;
}
}
public class FileOperation
{
private String FileRead(FileStream Fs)
{
StreamReader Sr = new StreamReader(Fs);
Sr.BaseStream.Seek(0, SeekOrigin.Begin);
String str = ""+(Char)Sr.Read();
String ret = "";
while (!Sr.EndOfStream)
{
ret += str;
str = ""+(Char)Sr.Read();
}
Sr.Close();
return ret;
}
public String FileRead(String FileName, FileMode Fm, FileAccess Fa)
{
FileOperation Fo = new FileOperation();
FileStream Fs = new FileStream(FileName, Fm, Fa);
String ret = Fo.FileRead(Fs);
Fs.Close();
return ret;
}
}
}
我正在使用Visual Studio 2005.此Console.ReadLine()
和Console.Read()
函数在此程序中无法正常运行。为什么呢?
答案 0 :(得分:5)
您可能想尝试Console.ReadKey(true),等待下一次按键然后继续,该函数返回ConsoleKeyInfo。在引用Key属性中的ConsoleKey值时,可以使用switch语句:
var c = Console.ReadKey();
Console.WriteLine();
Console.WriteLine(c.Key.ToString());
// Prints
//a
//A
switch(c.Key)
{
case ConsoleKey.D0:
//User entered 0
Console.WriteLine("Exiting...");
break;
case ConsoleKey.D1:
//User entered 1
Console.WriteLine("You chose to create a new file!");
break;
case ConsoleKey.D2:
//User entered 2
Console.WriteLine("You chose to open a file!");
break;
case ConsoleKey.D3:
//User entered 3
Console.WriteLine("You chose to edit an existing file!");
break;
default:
Console.WriteLine("No response for that key");
break;
}
Console.ReadLine();
答案 1 :(得分:0)
将Console.Read()
方法替换为Console.ReadLine()
。详情了解Console.Read()
中的Console.ReadLine()
和MSDN方法。编辑的程序如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FileOperation1
{
class FileMain
{
static void Main(string[] args)
{
FileMain fm = new FileMain();
char ch = fm.Menu();
while (ch != '0')
{
switch (ch)
{
case '0':
break;
case '1':
//Console.WriteLine("This featute is not implemented till now.");
break;
case '2':
Console.Write("Enter the name of the file: ");
String FileName = Console.ReadLine();// ReadLine() method is not workin here
FileOperation Fo = new FileOperation();
Console.WriteLine("\n" + Fo.FileRead(FileName, FileMode.Open, FileAccess.Read));
break;
case '3':
//Console.WriteLine("This featute is not implemented till now.");
break;
default:
Console.WriteLine("Invalid choice. Enter again.");
break;
}
ch = fm.Menu();
}
}
private char Menu()
{
Console.WriteLine("\n\t***File Operations***");
Console.WriteLine("1. Create a new file");
Console.WriteLine("2. Open a file");
Console.WriteLine("3. Edit an existing file");
Console.WriteLine("0. Exit");
Console.Write("\nEnter your choice: ");
char ch = Convert.ToChar(Console.ReadLine()); //Read() Method is not working properly
return ch;
}
}
public class FileOperation
{
private String FileRead(FileStream Fs)
{
StreamReader Sr = new StreamReader(Fs);
Sr.BaseStream.Seek(0, SeekOrigin.Begin);
String str = "" + (Char)Sr.Read();
String ret = "";
while (!Sr.EndOfStream)
{
ret += str;
str = "" + (Char)Sr.Read();
}
Sr.Close();
return ret;
}
public String FileRead(String FileName, FileMode Fm, FileAccess Fa)
{
FileOperation Fo = new FileOperation();
FileStream Fs = new FileStream(FileName, Fm, Fa);
String ret = Fo.FileRead(Fs);
Fs.Close();
return ret;
}
}
}