Home All Groups Group Topic Archive Search About
Author
6 Jul 2006 7:57 AM
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

Author
6 Jul 2006 8:18 AM
Adamz5
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:
Show quoteHide quote
> 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
Author
6 Jul 2006 8:39 AM
Jack Russell
Adamz5 wrote:
Show quoteHide quote
> 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
Author
6 Jul 2006 8:56 AM
Adamz5
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
Author
6 Jul 2006 9:16 AM
Simon Verona
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

Show quoteHide quote
"Adamz5" <ada***@hotmail.com> wrote in message
news:1152176207.001859.17850@b68g2000cwa.googlegroups.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
>
Author
6 Jul 2006 9:23 AM
Adamz5
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
Author
6 Jul 2006 11:31 AM
Jack Russell
Adamz5 wrote:
Show quoteHide quote
> 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
Author
7 Jul 2006 8:37 PM
GhostInAK
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

Show quoteHide quote
> 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
>
Author
6 Jul 2006 9:32 AM
Patrice
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.au> a écrit dans le message de news:
OVm9THNoGHA.4***@TK2MSFTNGP03.phx.gbl...
Show quoteHide quote
> 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
Author
6 Jul 2006 2:14 PM
Claes Bergefall
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


Show quoteHide quote
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:OVm9THNoGHA.4172@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
Author
6 Jul 2006 9:48 PM
Jack Russell
Claes Bergefall wrote:
Show quoteHide quote
> 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.au> wrote in message
> news:OVm9THNoGHA.4172@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
Author
6 Jul 2006 10:00 PM
Jack Russell
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:
Show quoteHide quote
> 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.au> wrote in message
> news:OVm9THNoGHA.4172@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
>
>
>
Author
7 Jul 2006 8:04 AM
Göran_Andersson
Use the array of invalid characters from the Path class:

If fileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) <> -1 Then


Jack Russell wrote:
Show quoteHide quote
> 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
Author
7 Jul 2006 10:27 AM
Herfried K. Wagner [MVP]
"Göran Andersson" <gu***@guffa.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Jul 2006 11:43 AM
C-Services Holland b.v.
Göran Andersson wrote:

> 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.



--
Rinze van Huizen
C-Services Holland b.v