tags:

views:

804

answers:

8

I'm something of a n00b at C# and I'm having trouble finding an answer to this, so if it's already been answered somewhere feel free to laugh at me (provided you also share the solution). :)

I'm reading an XML file into a GUI form, where certain elements are paths to files that are entered into TextBox objects. I'm looping through the controls on the form, and for each file path in each TextBox (lol there's like 20 of them on this form), I want to use File.Exists() to ensure it's a valid file.

The problem with this is that the file path can potentially contain spaces, and can potentially be valid; however File.Exists() is telling me it's invalid, based entirely on the spaces. Obviously I can't hard-code them and do something like

if (File.Exists(@"c:\Path To Stuff"))

and I tried surrounding the path with ", like

if (File.Exists("\"" + contentsOfTextBox + "\""))

but that didn't make a difference. Is there some way to do this? Can I escape the spaces somehow?

Thank you for your time. :)

+3  A: 

File.Exists works just fine with spaces. There is something else giving you a problem I'll wager.

Make sure your XML reader isn't failing to read the filename (parts of XML do not allow spaces and some readers will throw an exception if they encounter one).

Ron Warholic
So when File.Exist() returns false, I've got a MessageBox popping up with the full file path that apparently doesn't exist. The XML reader is reading in the filenames just fine. Also, I'm able to fill my TextBox objects with the filenames from the XML, and they're fine too; it's just this validation that's causing the problem. Good thinking though. :\
dmnapolitano
Are you sure the file exists? Make sure you're using Directory.Exists if you expect a folder name.
Ron Warholic
I just saw your reply lower. The only other thing I can think of is your permissions settings. From the docs:If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.
Ron Warholic
A: 

Did you remember to replace \ with \\ ?

Vivek
`@"c:\Path To Stuff"` is a verbatim string and doesn't require you to escape backslashes. See http://msdn.microsoft.com/en-us/library/aa691090%28VS.71%29.aspx
R. Bemrose
He's not using that syntax in the second example.
adrianbanks
He's not using the syntax but the "\" will not be escape characters as they are already in a variable.
Marcel Gosselin
The 2nd example is really what I'm dealing with. I tried doing a replace of "\" with "\\"; the replace was fine but it didn't make a difference. Do I have to do anything like replace C:\ with C:/?
dmnapolitano
Also, the debugger shows me that C# already replaced \ with \\.
dmnapolitano
+2  A: 

As the answer said, File.Exists works with spaces, if you are checking for existence of a Directory however, you should be using Directory.Exists

Tim
A: 

You need to use youtStringValue.Trim() to remove spaces leading/trailing, and Replace to remove spaces in the string you do not want.

Also, rather use System.IO.Path.Combine rather to combine these strings.

astander
File.Exists trims the trailing spaces for you. And how would removing spaces using Replace help, as that would end up with something like C:\ProgramFiles\... which would not exist?
adrianbanks
If from the textbox, the user entered spaces that was not intended, it can be removed.
astander
+1  A: 

What is the exact error that you get when File.Exists says it is invalid?

I suspect that you are passing a path to a directory and not a file, which will return false. If so, to check the presence of a directory, use Directory.Exists.

adrianbanks
+2  A: 

@"c:\Path To Stuff"

The above could be a directory not a file!

Hence you would want to use Directory.Exists!

@"c:\Path To Stuff\file.txt"

If you did have a file on the end of the path then you would use File.Exists!

Actually, "Path To Stuff" _could_ be a file and not a directory. It's pretty rare to have a file without an extension, but it's permissible in every file system I've ever used (and I've used quite a few).
Gregory Higley
Thanks Gregory; yeah that was a bad example on my part, lol. It's really c:\Path To Stuff\file.txt. I guess my brain wishes it were still in UNIX-land where there typically aren't file extensions :)
dmnapolitano
A: 

hi this is not difficult if you can convert the name of the path to a string array then go through one by one and remove the spaces

once that is done just write() to the screen where you have the files, if it is xml then your xmlmapper will suffice

file.exists() should only be used in certain circumstances if you know that it does exist but not when there can be space chars or any other possible user input

someone please upvote for a good answer

jimmy p
A: 

You can use @ on string variables:

string aPath = "c:\Path To Stuff\text.txt";
File.Exists(@aPath);

That should solve any escape character problems because I don't think this really looks like the spaces being the problem.

Niklas Winde