473,698 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating file names

Are there any methods that indicate whether a string can be used as a full path for a file? For example, what I'm looking for is a method that would test for things like correct file name format, invalid characters, total number of characters (to make sure the path is not too long), existing local drive letters, etc. Note that the file does not have to exist. Even a method that only tests some of these items would be useful. The best solution that I can come up with is to attempt to create a FileInfo in a Try-Catch-End Try statement. If an exception is thrown then the specified path is invalid. Of course this isn't very good because it requires an exception to be thrown (nor does it test everything that I would like to test)

Thanks for any help
Lance
Nov 20 '05 #1
4 1419
Cor
Hi Lance,

With this you should have to find your solution I think.
(I did not really use it as you would, but I think it should be posible with
that)

http://msdn.microsoft.com/library/de...mberstopic.asp

I hope this helps?

Cor
Are there any methods that indicate whether a string can be used as a full path for a file? For example, what I'm looking for is a method that would
test for things like correct file name format, invalid characters, total
number of characters (to make sure the path is not too long), existing local
drive letters, etc. Note that the file does not have to exist. Even a
method that only tests some of these items would be useful. The best
solution that I can come up with is to attempt to create a FileInfo in a
Try-Catch-End Try statement. If an exception is thrown then the specified
path is invalid. Of course this isn't very good because it requires an
exception to be thrown (nor does it test everything that I would like to
test).
Thanks for any help.
Lance

Nov 20 '05 #2
* "Cor" <no*@non.com> scripsit:
With this you should have to find your solution I think.
(I did not really use it as you would, but I think it should be posible with
that)

http://msdn.microsoft.com/library/de...mberstopic.asp


Mhm...

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Website Address Changed!
Nov 20 '05 #3
Lance,
Short of actually trying to create or open the file, I do not know of any
function to validate a file name.

A quick search of MSDN did not offer any real help either.

Part of the problem you are going to have with said function: is that your
A: running FAT16 may (will) have different rules, then your C: running
FAT32, from your D: running CDFS, from your E: running NTFS, from your F:
running Novel, from your G: running Linux file system, from your H: running
IFS, from your.... (I'm sure there are more types of file systems
supported!)

Also remember a number of functions accept a UNC path, a URI path, or even a
URL path to "open" the file.

NOTE: I am not saying one does not exist! I am saying that I do not know of
one. I am also saying if you create one, remember that different files
systems (readily available as network drives) have different rules for file
names.

I would consider using a System.Test.Reg ularExpressions .RegEx to create a
pattern that would match most "commonly used" file names and limit names to
that subset. However more then likely I would simply attempt to create/open
file file and handle any exceptions that were thrown.

Hope this helps
Jay

"Lance" <zi***@hotmail. com> wrote in message
news:A8******** *************** ***********@mic rosoft.com...
Are there any methods that indicate whether a string can be used as a full path for a file? For example, what I'm looking for is a method that would
test for things like correct file name format, invalid characters, total
number of characters (to make sure the path is not too long), existing local
drive letters, etc. Note that the file does not have to exist. Even a
method that only tests some of these items would be useful. The best
solution that I can come up with is to attempt to create a FileInfo in a
Try-Catch-End Try statement. If an exception is thrown then the specified
path is invalid. Of course this isn't very good because it requires an
exception to be thrown (nor does it test everything that I would like to
test).
Thanks for any help.
Lance

Nov 20 '05 #4
Ok, I think I'll use your suggestion of catching and handling exceptions. Thanks.
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14006
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it dosent meet these requirments an error message will be displayed. I have pasted the code (and highlighted the relevant parts) below in the hope that someone can help me out with this. Ive been trying to suss it out all week & it's driving me nuts!...
1
4330
by: Christian | last post by:
Hi, I load an Xml-file "customers.xml" into a DataSet (works fine) but then how do I validate it against a schema (e.g. customers.xsd) ? my customers.xml: <?xml version="1.0" encoding="utf-8"?>| <customers xmlns="http://tempuri.org/customers.xsd"> <Customer ID="1000"> <FirstName>Greg</FirstName>
2
2124
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating event, I check that the string in the textbox is a file that exists or whether or not the string is blank and display a message box in either case. I also call e.Cancel so that the value will be corrected. However, certain buttons on the form...
7
12946
by: Tizzah | last post by:
What is wrong with that? regex = /^(http|https):\/\/+({1}+)*\.{2,5}(({1,5})?\/.*)?$/ if(field.hpage.value != regex.test(field.hpage.value)){ alert("Bad Homepage") field.hpage.focus() field.hpage.select() return false
7
1755
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its events do this for me, but my extended textbox doesn't get created by those wonderful form setup wizards. So, 1) Is there a way I can pick up these events without having to code for each control and without using custom extended controls, OR
3
3235
by: vhrao | last post by:
I am trying to validate a xml file with two schema files cust.xsd and cust1.xsd. The schema file cust.xsd allows addition of elements from another schema cust1.xsd by using xs:any wildcard. cust.xsd - schema <?xml version="1.0" encoding="utf-8" ?> <xs:schema targetNamespace="urn:xmlns:sysapex-com:customer" elementFormDefault="qualified" xmlns="urn:xmlns:sysapex-com:customer" xmlns:mstns="urn:xmlns:sysapex-com:customer"
3
1460
by: mathieu | last post by:
Hi there, Could someone please suggest an open source implementation of a validating XML parser ? I am interested in how this thing can (should?) be designed. In particular how the errors should be handle: 1. If one at a time, it make is difficult to handle from a GUI, since only one error can be corrected at a time 2. All errors are reported, which means that false positive may be reported Also how does the implementation report which...
76
3062
by: Michael Stemper | last post by:
The W3C Validator is a great help, as far as it goes. However, I'm looking for something stricter. My coding style does not allow for implicit termination of an element; my intention and desire is to explicitly terminate every element. Just last night, it took me two hours to track down some funny behavior. It turned out to be caused by entries in a definition list that looked like: <dt>Term<dt> <dd>Definition</dd>
0
8601
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
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7716
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...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
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...
1
3043
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
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
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.