473,819 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Valid file name

Is there a simple function to test if a string is a valid file name (i.e
does not contain illegal characters etc) other than doing it the long way?

Thanks

Jack Russell
Jul 6 '06
14 4298
Claes,

That works for some characters but not all ? and / are not valid but it
does not throw an exception for them.

Jack
Claes Bergefall wrote:
Try Path.GetFileNam e or Path.GetDirecto ryName (or both). Both will throw an
exception if invalid characters are found in the string. If you need a list
of invalid characters you can get them by using Path.GetInvalid FileNameChars
and Path.GetInvalid PathChars

/claes
"Jack Russell" <ja***@norubbis h.tpg.com.auwro te in message
news:OV******** ******@TK2MSFTN GP03.phx.gbl...
>>Is there a simple function to test if a string is a valid file name (i.e
does not contain illegal characters etc) other than doing it the long way?

Thanks

Jack Russell


Jul 6 '06 #11
Use the array of invalid characters from the Path class:

If fileName.IndexO fAny(System.IO. Path.GetInvalid PathChars()) <-1 Then
Jack Russell wrote:
Is there a simple function to test if a string is a valid file name (i.e
does not contain illegal characters etc) other than doing it the long way?

Thanks

Jack Russell
Jul 7 '06 #12
"Göran Andersson" <gu***@guffa.co mschrieb:
Use the array of invalid characters from the Path class:

If fileName.IndexO fAny(System.IO. Path.GetInvalid PathChars()) <-1 Then
ACK, but note that this won't guarantee the validity of the filename because
'GetInvalidPath Chars' doens't necessarily return all invalid path characters
and even a path that doesn't contain any of these characters still may not
be valid.

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

Jul 7 '06 #13
Göran Andersson wrote:
Use the array of invalid characters from the Path class:

If fileName.IndexO fAny(System.IO. Path.GetInvalid PathChars()) <-1 Then

This would qualify:
"c:\foo\::bar:\ d\\foo" as valid. The above method will only tell you if
it contains invalid characters, but it will not tell you if a path is
actually a valid path.

--
Rinze van Huizen
C-Services Holland b.v
Jul 7 '06 #14
Hello Jack,

On XP SP1 or 2k3 there is: CheckNameLegalD OS8Dot3() exported by Kernel32.dll.
It obviously does not work on long filenames.. so don't forget about GetShortPathNam e()

-Boo
Adamz5 wrote:
>Okay here you go...
full code to check file name.
'place a textbox on form write text into it and then press button 1
'place this code in the button1_click event to check that the
filename entered in textbox 1 is fine

Dim uservalue As String
uservalue = TextBox1.Text
Dim b As Boolean = False
b = uservalue.Conta ins("/") Or uservalue.Conta ins("?") _
Or uservalue.Conta ins(":") _
Or uservalue.Conta ins("*") _
Or uservalue.Conta ins("""") _
Or uservalue.Conta ins("<") _
Or uservalue.Conta ins(">") _
Or uservalue.Conta ins("|") _
Or uservalue.Conta ins("?")
If b = False Then
MsgBox("Vali d Filename")
Else
MsgBox("Invali d Filename")
End If
By the way this is the long way (not that long is it?)

Anyway hope this helps

Regards

Adam
Yes that is basically what I did but knowing MS there are other things
that make a file name invalid so I thought there might be a system
function that checked all possibilities. Anyway thanks for everyones
thoughts.

Jack

Jul 7 '06 #15

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

Similar topics

3
14540
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save dialog. The strange thing is that sometimes the save dialogue
5
1766
by: Sky | last post by:
What makes something a valid DataSource? What methods/iterators/etc? Why do I ask? I do understand that a DataSet is based on an XML structure...but it's too table structured for what I am thinking... Can one read in a an xml file that has various embedded nodes (ie: records that have children records as XML does best) -- possibly not all the same length (ie, dif 'column count' for certain 'rows') and try to shove that into a...
1
6153
by: David Lozzi | last post by:
Hello, My webservice is receiving the following error. It runs fine on my local development machine,but when moved to production it errors. I found one KB about this error but it was in regards to .Net 1.0, this server is 1.1. Thanks! Configuration Error Description: An error occurred during the processing of a configuration file
1
14473
by: Andy | last post by:
Hi, I created a sample web service and tried calling it from my VBScript. Here it is: ---------------------------------------------------------------------------- --------------- '-- Declare variables dim fs
10
16434
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not recognized as a valid Boolean. Public Sub UpdateCustomer_DashboardGraphs(ByVal sender As Object, ByVal e As System.EventArgs)
4
5570
by: Jatinder | last post by:
Hi, I am trying to grant connect privilege to a user present on my O.S. (Windows) using following statement. GRANT CONNECT ON DATABASE TO user "user1" now when I execute this statemnt from db2 clp(ofcourse prefixing db2) it works fine, but when I put it inside an sql file and then when I try to run the file using db2 -tvf, it gives me following error msg
10
4275
by: SpreadTooThin | last post by:
Hi I'm writing a python script that creates directories from user input. Sometimes the user inputs characters that aren't valid characters for a file or directory name. Here are the characters that I consider to be valid characters... valid = ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' if I have a string called fname I want to go through each character in
2
10294
by: yogitha | last post by:
For the below code i am getting an error like : --------------------------------------------------------------- Warning: fread(): supplied argument is not a valid stream resource in D:\Public_Site\multifileupload\welcome_addfiles.php on line 28 ------------------------------------------------------------ i am trying many ways by giving diff arguments in fread() --------------------------------------------------- suggest me , wat error...
10
5942
by: Academia | last post by:
I'd like to check a string to see that it is a valid file name. Is there a Like pattern or RegEx that can do that. 1) Just the file name with maybe an extension 2)A full path An help for either of the above would be appreciated.
0
9748
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10702
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
10159
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
9247
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
7700
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
6910
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
5589
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
4380
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
3915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.