473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 #1
14 4237
Hi Jack,

Simply do a check to see if file.exists

if file exists it is a valid path if not path was invalid.

--Or to open a file -- prompt the user with a file dialogue box.

hope this helps

Adam

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 6 '06 #2
Adamz5 wrote:
Hi Jack,

Simply do a check to see if file.exists

if file exists it is a valid path if not path was invalid.

--Or to open a file -- prompt the user with a file dialogue box.

hope this helps

Adam

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

I tried file exists (before posting) with an invalid name - a/b.txt and
it did not throw an exception (much to my surprise)

Jack
Jul 6 '06 #3
try this

regards

Adam

Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Dim f As File

If File.Exists("c:\a/b.txt ") Then
MsgBox("validpath")
Else
MsgBox("invalidpath")
End If

End Sub
End Class

Jul 6 '06 #4
This would report an invalid path even if the filename is syntactically
correct but just doesn't exist!

Simon

--
================================
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"Adamz5" <ad****@hotmail.comwrote in message
news:11*********************@b68g2000cwa.googlegro ups.com...
try this

regards

Adam

Imports System.IO

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Dim f As File

If File.Exists("c:\a/b.txt ") Then
MsgBox("validpath")
Else
MsgBox("invalidpath")
End If

End Sub
End Class

Jul 6 '06 #5
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.Contains("/") Or uservalue.Contains("?") _
Or uservalue.Contains(":") _
Or uservalue.Contains("*") _
Or uservalue.Contains("""") _
Or uservalue.Contains("<") _
Or uservalue.Contains(">") _
Or uservalue.Contains("|") _
Or uservalue.Contains("?")

If b = False Then
MsgBox("Valid Filename")
Else
MsgBox("Invalid Filename")
End If

By the way this is the long way (not that long is it?)

Anyway hope this helps

Regards

Adam

Jul 6 '06 #6
Which version ?

2.0 has a System.IO.Path.GetFullPath that should raise an Argument exception
if the path contains invalid chars (the same class exposes info about
forbidden chars for file names and paths).

--
Patrice

"Jack Russell" <ja***@norubbish.tpg.com.aua écrit dans le message de news:
OV**************@TK2MSFTNGP03.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 #7
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.Contains("/") Or uservalue.Contains("?") _
Or uservalue.Contains(":") _
Or uservalue.Contains("*") _
Or uservalue.Contains("""") _
Or uservalue.Contains("<") _
Or uservalue.Contains(">") _
Or uservalue.Contains("|") _
Or uservalue.Contains("?")

If b = False Then
MsgBox("Valid Filename")
Else
MsgBox("Invalid 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 6 '06 #8
Try Path.GetFileName or Path.GetDirectoryName (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.GetInvalidFileNameChars
and Path.GetInvalidPathChars

/claes
"Jack Russell" <ja***@norubbish.tpg.com.auwrote in message
news:OV**************@TK2MSFTNGP03.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 #9
Claes Bergefall wrote:
Try Path.GetFileName or Path.GetDirectoryName (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.GetInvalidFileNameChars
and Path.GetInvalidPathChars

/claes
"Jack Russell" <ja***@norubbish.tpg.com.auwrote in message
news:OV**************@TK2MSFTNGP03.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


Thanks for that
Jul 6 '06 #10
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.GetFileName or Path.GetDirectoryName (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.GetInvalidFileNameChars
and Path.GetInvalidPathChars

/claes
"Jack Russell" <ja***@norubbish.tpg.com.auwrote in message
news:OV**************@TK2MSFTNGP03.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.IndexOfAny(System.IO.Path.GetInvalidPathC hars()) <-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.comschrieb:
Use the array of invalid characters from the Path class:

If fileName.IndexOfAny(System.IO.Path.GetInvalidPathC hars()) <-1 Then
ACK, but note that this won't guarantee the validity of the filename because
'GetInvalidPathChars' 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.IndexOfAny(System.IO.Path.GetInvalidPathC hars()) <-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: CheckNameLegalDOS8Dot3() exported by Kernel32.dll.
It obviously does not work on long filenames.. so don't forget about GetShortPathName()

-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.Contains("/") Or uservalue.Contains("?") _
Or uservalue.Contains(":") _
Or uservalue.Contains("*") _
Or uservalue.Contains("""") _
Or uservalue.Contains("<") _
Or uservalue.Contains(">") _
Or uservalue.Contains("|") _
Or uservalue.Contains("?")
If b = False Then
MsgBox("Valid Filename")
Else
MsgBox("Invalid 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
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...
5
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...
1
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...
1
by: Andy | last post by:
Hi, I created a sample web service and tried calling it from my VBScript. Here it is: ---------------------------------------------------------------------------- --------------- '-- Declare...
10
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...
4
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...
10
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...
2
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...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.