473,890 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to validate a file name in vb.net?

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
5 11984
"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
Make sure it doesn't contain Path.InvalidPat hChars or isn't = "".
"Darious Snell" <nu**@sdlkfweu. org> wrote in message
news:11******** *****@corp.supe rnews.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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10159
by: JonChart | last post by:
Hi all, I would like to know ho to validate a file name when uploading via asp and whether or not this can be validated with javascript? I need to upload 2 images and the file names must be the same... 1) browse for 1st image. 2) image path and file name appears in text box i.e. IMAGE1.JPG 3) browse for 2nd image.
6
9189
by: Allen | last post by:
Hi, I want to retrieve the selected folder and entered file name from the SaveFilsDialog. The user is able to create or selected a different folder during the dialog displayed. If user enters "a/b/c/d" in the "File name:" box, then the SaveFileDialog.FileName will return "Path\a\b\c\d" as the name. Is there any way to just retrieve the selected folder and entered file name form the SaveFiledialog?
1
1384
by: Gaetan | last post by:
I have a web application where the user can type the name of a file name in a field. I want to make sure the syntax of that filename is valid before using it. I browsed the methods in various .Net 2.0 framework assemblies and there does not seem to be any to validate the syntax of a string representing a filename. Is there a method I can use to validate the syntax of a NTFS file name? regards,
3
18100
by: sheeeng | last post by:
Hi all, I know that we can check for invalid file name characters as shown at http://www.codeproject.com/useritems/reallyusefulpath.asp. But how can we check a string for invalid file name such as CLOCK$, AUX, CON, NUL, PRN, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9? There is a C++ implementation of it at
9
5432
by: Sam | last post by:
Validate domain name context Hello , Today I want to make sure from a given text is a domain style or not. for example : the domain can be example.com or example.com.us or example.us so , what is the pattren to check the given string is a domain style or not?
2
9295
by: harry | last post by:
I need to validate a file name but it has to work on Windows & Unix! Does anybody know of java code that does this? thanks in advance harry
4
7966
by: carmelo | last post by:
Hi! I need to read and store data from a file, but I don't the name of it; Is this code correct and is there a better way to do this thing?? int main() { cout << " type file name:" char * name; cin >name;
185
7175
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will return the file name given a FILE *. Questions: What would be the best name for this function?
3
2484
by: laredotornado | last post by:
Hi, Can anyone provide a function that can validate if a string would pass as a valid Unix (specifically Fedora Core 8 Linux) file name? For example, I'm fairly certain one of the rules is that the file name cannot contain a "&" character. There are probably a lot of other rules I don't know about. Thanks, - Dave
2
3261
by: lenniekuah | last post by:
Hullo Good Guys, I have an intesting problem and I need your help. Please help me. I am using Vb.NET2008 to develop Window Application. When the user enter the File Name on the textbox, I need to check the file name to ensure that it has the File Extension. (Eg. Sales.XLS) I have not done the coding before and stuggling with it. Please help me with source coding. Thank You. Cheers,
0
9826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.