C# 尝试在c中向用户输入添加某种形式的验证#
我正在尝试用C#创建一个能够读取.TXT文件的简单程序。到目前为止,如果键入了错误的文件位置,我能够显示错误消息,但是当我输入正确的文件路径时,程序不会显示任何内容。。。如有任何建议/指导,将不胜感激。谢谢C# 尝试在c中向用户输入添加某种形式的验证#,c#,C#,我正在尝试用C#创建一个能够读取.TXT文件的简单程序。到目前为止,如果键入了错误的文件位置,我能够显示错误消息,但是当我输入正确的文件路径时,程序不会显示任何内容。。。如有任何建议/指导,将不胜感激。谢谢 `using System; using System.IO; class ReadFromFile { static void Main() { Console.WriteLine ("Welcome to Decrypter (Press any
`using System;
using System.IO;
class ReadFromFile
{
static void Main()
{
Console.WriteLine ("Welcome to Decrypter (Press any key to begin)");
Console.ReadKey ();
//User selects file they wish to decrypt
int counter = 0;
string line;
string path;
Console.WriteLine ("\nPlease type the path to your file");
path = Console.ReadLine ();
try
{
if (!File.Exists(path))
{
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(path);
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
}
}
catch (Exception)
{
Console.WriteLine("The value you entered is incorrect.");
}
}
}`试试这个
using System;
using System.IO;
namespace ConsoleApplication1
{
class ReadFromFile
{
static void Main()
{
Console.WriteLine("Welcome to Decrypter (Press any key to begin)");
Console.ReadKey();
//User selects file they wish to decrypt
int counter = 0;
string line;
string path = "";
try
{
while (true)
{
if (!File.Exists(path))
{
Console.WriteLine("\nPlease type the path to your file");
path = Console.ReadLine();
}
else
{
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line);
counter++;
}
file.Close();
break;
}
}
}
catch (Exception)
{
Console.WriteLine("The value you entered is incorrect.");
}
}
}
}
如果您只是尝试使用调试器遵循此代码,就很容易发现这一点!说明Notepad++中没有调试器文件中有任何内容吗?然后使用LinqPAD,或下载Visual Studio的免费版本。作为一名程序员,你应该选择有助于你工作的工具(顺便说一句,记事本++有一个用于CScript之类的插件)谢谢Steve。。。。。我看到了错误。。。。我花了好几个小时盯着它看。再次感谢。