visual studio 2008 - C# Can't find file

问题描述:

I have a C# Visual Studio 2008 Form which needs to read the contents of the relative file 'character/attacks.txt' File.Exists() returns false when I run it, even though I'm positive my directories are sorted. Code:

I'm porting my code from Python and never had any troubles with this. Thanks in advance!

网友答案:

That's very strange. The computer seems firmly convinced that there is no attacks.txt file in the D:\Users\Andrey\Desktop\Turn\character\ directory, but you say that it definitely exists. One of you must be wrong, and I've learned over the years that computers are right more often than I am.

Are you sure that your file actually has a .txt extension, not a .txt.somethingelse extension? If you have the display of file extensions turned off in the Windows shell, you might be missing this extra file extension. The computer isn't missing it internally, though, and it's seeing that as a totally different file than the one you requested. That's the same problem this guy had.

To re-configure Windows Explorer:

Open the Control Panel folder.

Click on "Folder Options".

Switch to the "View" tab.

Find the "Show hidden files, folders, and drives" radio button in the list of items in the "Advanced settings" list box, and make sure that it is selected.

Click OK.

The other answers to this question do make some good suggestions. Among them:

Make sure that if you use backslashes (which is the standard Windows path separator character) in your string literals, you "escape" them using a second backslash. The reason this is required is that the backslash is interpreted as an escape character by the C# compiler, which allows you to type things like \t to insert a tab. If you want to insert a regular backslash, you have to escape the escape—\\. That's the reason you were getting an error when you tried to use a single backslash.

Instead of escaping the backslash character, you can tell the C# compiler to interpret your string literal exactly as it is typed, including spaces. These are called verbatim string literals. You do this by prefixing the string with the @ character. For example: @"C:\MyDirectory\MyFile.txt"

The reason why the forward slash character that you're using now works is strictly for backwards-compatibility reasons. It isn't the cause of the error (as you can see from the exception message, which includes the path being searched), but it's probably still not a good idea to use forward slashes in paths. As I mentioned above, the backslash is the standard path delimiter in Windows.

网友答案:

I think because your file name has a "\" in it it might be messing it up and telling it to look in the character folder for the attacks.txt file.

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

网友答案:

Yeah if you replace the '/' with an '\' u ll get an invalid character error.
Try this with 2 slashes