Home All Groups Group Topic Archive Search About

Is there a Filename.IsValid function anywhere?

Author
16 Apr 2006 10:39 PM
dgk
I can't find anything in the framework that will tell me whether a
filename is valid. I suppose that I can just try to open it and trap
an error but that seems wasteful. I dug around and found this code:


  Public Function IsValidName(ByVal name As String) As Boolean
    Dim i As Integer
    For i = 0 To name.Length - 1
      Dim ch As Char = name.Chars(i)
      Dim uc As Globalization.UnicodeCategory =
[Char].GetUnicodeCategory(ch)
      Select Case uc
        Case Globalization.UnicodeCategory.UppercaseLetter,
Globalization.UnicodeCategory.LowercaseLetter,
Globalization.UnicodeCategory.TitlecaseLetter,
Globalization.UnicodeCategory.DecimalDigitNumber
        Case Else
          Return False
      End Select
    Next i
    Return True
  End Function

but this one failed on a name with a space in it, and that is valid.

Any suggestions?

Author
16 Apr 2006 11:05 PM
Jim Hughes
..NET Framework Class Library
Path.GetInvalidFileNameChars Method
Note: This method is new in the .NET Framework version 2.0.

http://msdn2.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars(VS.80).aspx


Show quoteHide quote
"dgk" <sonicechoes@zero-spam-hotmail.com> wrote in message
news:fjh542p7e2m4go4u0alvq9v6vt6ec7lup9@4ax.com...
>I can't find anything in the framework that will tell me whether a
> filename is valid. I suppose that I can just try to open it and trap
> an error but that seems wasteful. I dug around and found this code:
>
>
>  Public Function IsValidName(ByVal name As String) As Boolean
>    Dim i As Integer
>    For i = 0 To name.Length - 1
>      Dim ch As Char = name.Chars(i)
>      Dim uc As Globalization.UnicodeCategory =
> [Char].GetUnicodeCategory(ch)
>      Select Case uc
>        Case Globalization.UnicodeCategory.UppercaseLetter,
> Globalization.UnicodeCategory.LowercaseLetter,
> Globalization.UnicodeCategory.TitlecaseLetter,
> Globalization.UnicodeCategory.DecimalDigitNumber
>        Case Else
>          Return False
>      End Select
>    Next i
>    Return True
>  End Function
>
> but this one failed on a name with a space in it, and that is valid.
>
> Any suggestions?
Author
17 Apr 2006 12:22 AM
dgk
On Sun, 16 Apr 2006 16:05:34 -0700, "Jim Hughes"
<NOSPAMJ3033@Hotmail.com> wrote:

>.NET Framework Class Library
>Path.GetInvalidFileNameChars Method
>Note: This method is new in the .NET Framework version 2.0.
>
>http://msdn2.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars(VS.80).aspx
>
>

Thanks, that makes things easier. But all it returns is "<>| those are
the only invalid file characters? / and \ should be illegal. What
about ? and *

#%~`

Well, I guess that they're legal.
Author
17 Apr 2006 3:52 AM
Jim Hughes
Show quote Hide quote
"dgk" <NoWh***@MailsAnonymous.com> wrote in message
news:egn542hjnqh0alketta74v8boahe8aemph@4ax.com...
> On Sun, 16 Apr 2006 16:05:34 -0700, "Jim Hughes"
> <NOSPAMJ3033@Hotmail.com> wrote:
>
>>.NET Framework Class Library
>>Path.GetInvalidFileNameChars Method
>>Note: This method is new in the .NET Framework version 2.0.
>>
>>http://msdn2.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars(VS.80).aspx
>>
>>
>
> Thanks, that makes things easier. But all it returns is "<>| those are
> the only invalid file characters? / and \ should be illegal. What
> about ? and *
>
> #%~`
>
> Well, I guess that they're legal.


If all you did is a System.IO.Path.GetInvalidFileNameChars().ToString(), I
can see how you might think that.

However, there are 41 separate characters in that list on my system.

"
<
>
|
(additional unprintable characters stripped by me from reply):
:
*
?
\
/

  static void test()
  {
   char[] ch = System.IO.Path.GetInvalidFileNameChars();
   System.Diagnostics.Trace.WriteLine(ch.Length);
   for (int i = 0; i < ch.Length; i++)
   {
    // dump hex values for each char
    //System.Diagnostics.Trace.WriteLine(Convert.ToByte(ch[i]).ToString("X"));
    System.Diagnostics.Trace.WriteLine(ch[i]);
   }
  }

From the link I sent:

Remarks

The array returned from this method is not guaranteed to contain the
complete set of characters that are invalid in file and directory names. The
full set of invalid characters can vary by file system. For example, on
Windows-based desktop platforms, invalid path characters might include
ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<),
greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).
Author
17 Apr 2006 12:57 PM
dgk
On Sun, 16 Apr 2006 20:52:54 -0700, "Jim Hughes"
<NOSPAMJ3033@Hotmail.com> wrote:

Show quoteHide quote
>"dgk" <NoWh***@MailsAnonymous.com> wrote in message
>news:egn542hjnqh0alketta74v8boahe8aemph@4ax.com...
>> On Sun, 16 Apr 2006 16:05:34 -0700, "Jim Hughes"
>> <NOSPAMJ3033@Hotmail.com> wrote:
>>
>>>.NET Framework Class Library
>>>Path.GetInvalidFileNameChars Method
>>>Note: This method is new in the .NET Framework version 2.0.
>>>
>>>http://msdn2.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars(VS.80).aspx
>>>
>>>
>>
>> Thanks, that makes things easier. But all it returns is "<>| those are
>> the only invalid file characters? / and \ should be illegal. What
>> about ? and *
>>
>> #%~`
>>
>> Well, I guess that they're legal.
>
>
>If all you did is a System.IO.Path.GetInvalidFileNameChars().ToString(), I
>can see how you might think that.
>
>However, there are 41 separate characters in that list on my system.
>
>"
><
>>
>|
>(additional unprintable characters stripped by me from reply):
>:
>*
>?
>\
>/
>
>  static void test()
>  {
>   char[] ch = System.IO.Path.GetInvalidFileNameChars();
>   System.Diagnostics.Trace.WriteLine(ch.Length);
>   for (int i = 0; i < ch.Length; i++)
>   {
>    // dump hex values for each char
>    //System.Diagnostics.Trace.WriteLine(Convert.ToByte(ch[i]).ToString("X"));
>    System.Diagnostics.Trace.WriteLine(ch[i]);
>   }
>  }
>
>From the link I sent:
>
>Remarks
>
>The array returned from this method is not guaranteed to contain the
>complete set of characters that are invalid in file and directory names. The
>full set of invalid characters can vary by file system. For example, on
>Windows-based desktop platforms, invalid path characters might include
>ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<),
>greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).
>
>
>

I just hovered over the variable that I used and looked at the text
visualizer, which is bad form on my part. The length was 41 or so
however. Odd that many of the printable characters didn't show. I
guess because the unprintable ones were messing it up. Text Visualizer
really should have an option for hex.
Author
17 Apr 2006 2:30 PM
Hugh Janus
>
> I just hovered over the variable that I used and looked at the text
> visualizer, which is bad form on my part. The length was 41 or so
> however. Odd that many of the printable characters didn't show. I
> guess because the unprintable ones were messing it up. Text Visualizer
> really should have an option for hex.

Err....  Have I missed something, or could you not just use
File.Exists(Filename) ????
Author
18 Apr 2006 12:24 PM
dgk
On 17 Apr 2006 07:30:47 -0700, "Hugh Janus"
<my-junk-acco***@hotmail.com> wrote:

>>
>> I just hovered over the variable that I used and looked at the text
>> visualizer, which is bad form on my part. The length was 41 or so
>> however. Odd that many of the printable characters didn't show. I
>> guess because the unprintable ones were messing it up. Text Visualizer
>> really should have an option for hex.
>
>Err....  Have I missed something, or could you not just use
>File.Exists(Filename) ????

I just wanted to know if a text string would be a valid filename, not
whether it exists.
Author
19 Apr 2006 12:14 AM
Michael D. Ober
Take a look at system.io.path.  There are several member functions that
return path seperators, valid characters, etc.

Mike Ober.

Show quoteHide quote
"dgk" <d**@somewhere.com> wrote in message
news:uhm942lpdn4jv4gbkkpifb1ek2e2crmg6s@4ax.com...
> On 17 Apr 2006 07:30:47 -0700, "Hugh Janus"
> <my-junk-acco***@hotmail.com> wrote:
>
> >>
> >> I just hovered over the variable that I used and looked at the text
> >> visualizer, which is bad form on my part. The length was 41 or so
> >> however. Odd that many of the printable characters didn't show. I
> >> guess because the unprintable ones were messing it up. Text Visualizer
> >> really should have an option for hex.
> >
> >Err....  Have I missed something, or could you not just use
> >File.Exists(Filename) ????
>
> I just wanted to know if a text string would be a valid filename, not
> whether it exists.
>
Author
19 Apr 2006 3:20 AM
Barry
On Wed, 19 Apr 2006 00:14:16 GMT, "Michael D. Ober"
<ober***@.alum.mit.edu.nospam> wrote:

Show quoteHide quote
>Take a look at system.io.path.  There are several member functions that
>return path seperators, valid characters, etc.
>
>Mike Ober.
>
>"dgk" <d**@somewhere.com> wrote in message
>news:uhm942lpdn4jv4gbkkpifb1ek2e2crmg6s@4ax.com...
>> On 17 Apr 2006 07:30:47 -0700, "Hugh Janus"
>> <my-junk-acco***@hotmail.com> wrote:
>>
>> >>
>> >> I just hovered over the variable that I used and looked at the text
>> >> visualizer, which is bad form on my part. The length was 41 or so
>> >> however. Odd that many of the printable characters didn't show. I
>> >> guess because the unprintable ones were messing it up. Text Visualizer
>> >> really should have an option for hex.
>> >
>> >Err....  Have I missed something, or could you not just use
>> >File.Exists(Filename) ????
>>
>> I just wanted to know if a text string would be a valid filename, not
>> whether it exists.
>>
I've found that System.IO.Path.GetFullPath() will throw an exception
with a bad filename or a bad path.  I haven't found an invalid char
that it will accept.

These two will not necessarily pick up all the bad characters (for
example "*","?") :
System.IO.Path.GetFileName()
System.IO.Path.GetPathRoot()

I don't like expecting an exception to check for a bad value, but this
would be one way to check a valid filename.

HTH,
Barry

>
>

bceggersATcomcastDOTnet
Author
17 Apr 2006 8:12 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"dgk" <NoWh***@MailsAnonymous.com> schrieb:
>>.NET Framework Class Library
>>Path.GetInvalidFileNameChars Method
>>Note: This method is new in the .NET Framework version 2.0.
>>
>>http://msdn2.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars(VS.80).aspx
>>
>>
>
> Thanks, that makes things easier. But all it returns is "<>| those are
> the only invalid file characters? / and \ should be illegal. What
> about ? and *
>
> #%~`
>
> Well, I guess that they're legal.

The "Remarks" section is saying "The array returned from this method is not
guaranteed to contain the complete set of characters that are invalid in
file and directory names. The full set of invalid characters can vary by
file system".

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 Apr 2006 8:10 AM
Herfried K. Wagner [MVP]
"dgk" <sonicechoes@zero-spam-hotmail.com> schrieb:
>I can't find anything in the framework that will tell me whether a
> filename is valid. I suppose that I can just try to open it and trap
> an error but that seems wasteful.

I think that's a clean solution.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 Apr 2006 12:53 PM
dgk
On Mon, 17 Apr 2006 10:10:55 +0200, "Herfried K. Wagner [MVP]"
<hirf-spam-me-here@gmx.at> wrote:

>"dgk" <sonicechoes@zero-spam-hotmail.com> schrieb:
>>I can't find anything in the framework that will tell me whether a
>> filename is valid. I suppose that I can just try to open it and trap
>> an error but that seems wasteful.
>
>I think that's a clean solution.

It really works well. Trying to open a streamreader gives an "illegal
characters" error. If the name is valid it causes a "not found"
exception. Unless, I suppose, the file actually exists.

So be it.
Author
18 Apr 2006 12:11 PM
Andrew Morton
dgk wrote:
> I can't find anything in the framework that will tell me whether a
> filename is valid. I suppose that I can just try to open it and trap
> an error but that seems wasteful. I dug around and found this code:

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp
and note that CON etc. are not allowed but I suspect they might not generate
an error. You can create such a file, but accessing it may be problematic.

Andrew
Author
18 Apr 2006 12:26 PM
dgk
On Tue, 18 Apr 2006 13:11:03 +0100, "Andrew Morton"
<a**@in-press.co.uk.invalid> wrote:

>dgk wrote:
>> I can't find anything in the framework that will tell me whether a
>> filename is valid. I suppose that I can just try to open it and trap
>> an error but that seems wasteful. I dug around and found this code:
>
>See
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp
>and note that CON etc. are not allowed but I suspect they might not generate
>an error. You can create such a file, but accessing it may be problematic.
>
>Andrew
>

Thanks. It isn't as easy as the old days where the QuickPak
Professional library just had a ValidFilename function. Where is Ethan
Winer when I need him?