Is there a Filename.IsValid function anywhere?

  • Thread starter Thread starter dgk
  • Start date Start date
D

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?
 
dgk said:
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).ToString("X"));
System.Diagnostics.Trace.WriteLine(ch);
}
}

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).
 
dgk said:
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.
 
dgk said:
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".
 
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.
 
dgk said:
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).ToString("X"));
System.Diagnostics.Trace.WriteLine(ch);
}
}

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.
 
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) ????
 
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.
 
Take a look at system.io.path. There are several member functions that
return path seperators, valid characters, etc.

Mike Ober.
 
Take a look at system.io.path. There are several member functions that
return path seperators, valid characters, etc.

Mike Ober.
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
 
Back
Top