By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

How to validate a file name in vb.net?

P: n/a
I have a simple textbox. I want to make sure that the string typed in by a user is a
valid windows filename.
Can anyone point me to some classes that will help? Ideally, I guess I should create my
own textbox class that only allows the typing of a valid filename. Perhaps checking
against a regex on keypress?

Any help would be appreciated.

Thank you in advance.
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Darious Snell" <nu**@sdlkfweu.org> schrieb:
have a simple textbox. I want to make sure that the string typed in by a
user is a
valid windows filename.
Can anyone point me to some classes that will help? Ideally, I guess I
should create my
own textbox class that only allows the typing of a valid filename.
Perhaps checking
against a regex on keypress?


I doubt that you will come up with a stable solution. There are many
factors which influence whether or not a filename is valid. For example,
the user's privileges can be insufficient to access, modify, or create the
file, etc. Maybe using the OpenFileDialog component is an option.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
Make sure it doesn't contain Path.InvalidPathChars or isn't = "".
"Darious Snell" <nu**@sdlkfweu.org> wrote in message
news:11*************@corp.supernews.com...
I have a simple textbox. I want to make sure that the string typed in by a
user is a
valid windows filename.
Can anyone point me to some classes that will help? Ideally, I guess I
should create my
own textbox class that only allows the typing of a valid filename.
Perhaps checking
against a regex on keypress?

Any help would be appreciated.

Thank you in advance.

Nov 21 '05 #3

P: n/a
A really low tech way:
1) Check to see if the file exists. If it does, you have your answer.
2) If it doesn't exist, create the file. If it works, close and kill it,
you have your answer.
3) If it can't be created, you have your answer.

Bobbo
Nov 21 '05 #4

P: n/a
Hi Robert ! :O)
3) If it can't be created, you have your answer.


hmm... i'm not sure it's reliable.
There can be a whole lot of reasons for a file creation to fail. Look up the
Exception's types that can be thrown by the File.Create() method...

--
Best Regards
Yanick
Nov 21 '05 #5

P: n/a
Here is a link to a regular expression which validates file names.
Perhaps this will help you:

http://www.regexlib.com/REDetails.aspx?regexp_id=345

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.