开发者

Validating File Path w/Spaces in C#

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 surround开发者_如何学编程ing 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. :)


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).


@"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!


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


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.


To echo Ron Warholic: make sure the process has permissions over the target folder. I just ran into the same "bug" and it turned out to be a permissions issue.


Did you remember to replace \ with \\ ?


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜