Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在c中向用户输入添加某种形式的验证#_C# - Fatal编程技术网

C# 尝试在c中向用户输入添加某种形式的验证#

C# 尝试在c中向用户输入添加某种形式的验证#,c#,C#,我正在尝试用C#创建一个能够读取.TXT文件的简单程序。到目前为止,如果键入了错误的文件位置,我能够显示错误消息,但是当我输入正确的文件路径时,程序不会显示任何内容。。。如有任何建议/指导,将不胜感激。谢谢 `using System; using System.IO; class ReadFromFile { static void Main() { Console.WriteLine ("Welcome to Decrypter (Press any

我正在尝试用C#创建一个能够读取.TXT文件的简单程序。到目前为止,如果键入了错误的文件位置,我能够显示错误消息,但是当我输入正确的文件路径时,程序不会显示任何内容。。。如有任何建议/指导,将不胜感激。谢谢

`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。。。。。我看到了错误。。。。我花了好几个小时盯着它看。再次感谢。