How to validate a file name in vb.net?

D

Darious Snell

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

Herfried K. Wagner [MVP]

Darious Snell said:
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.
 
R

Robert S. Liles

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
 
Z

Zoury

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top