Valid file name

  • Thread starter Thread starter Jack Russell
  • Start date Start date
J

Jack Russell

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
 
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
 
Adamz5 said:
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
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
 
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
 
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
 
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
 
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).
 
Adamz5 said:
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
 
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
 
Claes said:
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
Thanks for that
 
Claes,

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

Jack
 
Use the array of invalid characters from the Path class:

If fileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) <> -1 Then
 
Göran Andersson said:
Use the array of invalid characters from the Path class:

If fileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) <> -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.
 
Göran Andersson said:
Use the array of invalid characters from the Path class:

If fileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) <> -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.
 
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
 
Back
Top