C # Can't find file

I have a C # Visual Studio 2008 form that needs to read the contents of the relative file 'character / attacks.txt' File.Exists () returns false on startup, although I'm pretty sure my directories are sorted, Code:

            try
            {
                System.IO.StreamReader file = new System.IO.StreamReader("character/attacks.txt");
                int counter = 0;
                int numberOfLines = 0;
                string line;
                while ((line = file.ReadLine()) != null) { numberOfLines++; }
                string[] attacks = new string[numberOfLines];
                while ((line = file.ReadLine()) != null) { attacks[counter] = line; counter++; }
                file.Close();
                print(attacks.ToString());
            }
            catch (Exception ex) 
            {
                print("ERROR"); print("Did you edit any files?"); print(ex.ToString()); 
            }

      

Exception error:

System.IO.FileNotFoundException: Could not find file 'D:\Users\Andrey\Desktop\Turn\character\attacks.txt'.
File name: 'D:\Users\Andrey\Desktop\Turn\character\attacks.txt'
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
   at System.IO.StreamReader..ctor(String path)
   at TurnByTurn.Form1.button1_Click(Object sender, EventArgs e) in D:\Users\Andrey\Desktop\C#\TurnByTurn\TurnByTurn\Form1.cs:line 52

      

I am porting my code from Python and have never had any problems with it. Thanks in advance!

+2


source to share


5 answers


It is very strange. The computer is sure there is no file attacks.txt

in the directory D:\Users\Andrey\Desktop\Turn\character\

, but you say that it definitely exists. One of you must be wrong, and I've learned over the years that computers work more often than I do.

Are you sure your file has an extension .txt

and not an extension .txt.somethingelse

? If the display of file extensions is disabled in the Windows shell, you may not need an additional file extension. However, the computer does not let it pass inside and it sees it as a completely different file than the one you requested. What the same problem > this guy had.

To reconfigure Windows Explorer:



  • Open the Control Panel folder.
  • Click Folder Options.
  • Click the View tab.
  • Locate the Show hidden files, folders, and drives radio button in the list of items in the Advanced Settings list, and make sure it is selected.
  • Click OK.

The other answers to this question do provide some helpful advice. Among them:

  • Make sure that if you use a backslash (which is the standard Windows path separator) in your string literals, you "escape" them by using a second backslash. The reason this is required is because the backslash is interpreted as an escape character by the C # compiler, which allows you to enter things like \t

    to insert a tab. If you want to insert a regular backslash, you need to escape the escape- \\

    . This is the reason you were getting an error when trying to use a single backslash.

  • Instead of stripping off the backslash character, you can tell the C # compiler to interpret the string literal exactly as it is printed, including spaces. They are called verbatim string literals . You do this by prefixing the string with a character @

    . For example:@"C:\MyDirectory\MyFile.txt"

  • The reason the forward slash character you are using now is strictly for backward compatibility reasons. This is not the cause of the error (as you can see from the exception message, which includes pathfinding), but it is probably not a good idea to use forward slashes in paths just yet. As I mentioned above, the backslash is the standard Windows path separator.

+4


source


I think because your filename has a "\" it might mess it up and tell it to look into the symbols folder for the attacks.txt file.



edit: or at least it looks like you are telling him that your file has a forward slash. Is the file path it gives you in error the actual location of the file on your computer?

0


source


Yes, if you replace "/" with "\" you get an invalid character error. Try to do it with two forward slashes

System.IO.StreamReader file = new System.IO.StreamReader("character\\attacks.txt");

      

or

System.IO.StreamReader file = new System.IO.StreamReader(@"character\attacks.txt");

      

0


source


0


source


This is because you created a text file, so it automatically has a .txt extension. So if you intentionally add .txt, then the file is now actually attack.txt.txt.

0


source







All Articles