Groups | Blog | Home
all groups > vb.net > april 2006 >

vb.net : Is there a Filename.IsValid function anywhere?



Jim Hughes
4/16/2006 4:05:34 PM
..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


[quoted text, click to view]

dgk
4/16/2006 6:39:30 PM
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.

Jim Hughes
4/16/2006 8:52:54 PM
[quoted text, click to view]


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.

"
<
[quoted text, click to view]
|
(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).



Herfried K. Wagner [MVP]
4/17/2006 12:00:00 AM
"dgk" <sonicechoes@zero-spam-hotmail.com> schrieb:
[quoted text, click to view]

I think that's a clean solution.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
Herfried K. Wagner [MVP]
4/17/2006 12:00:00 AM
"dgk" <NoWhere@MailsAnonymous.com> schrieb:
[quoted text, click to view]

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/>
dgk
4/17/2006 12:22:56 AM
On Sun, 16 Apr 2006 16:05:34 -0700, "Jim Hughes"
[quoted text, click to view]

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

#%~`

Hugh Janus
4/17/2006 7:30:47 AM
[quoted text, click to view]

Err.... Have I missed something, or could you not just use
File.Exists(Filename) ????
dgk
4/17/2006 8:53:57 AM
On Mon, 17 Apr 2006 10:10:55 +0200, "Herfried K. Wagner [MVP]"
[quoted text, click to view]

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.

dgk
4/17/2006 8:57:07 AM
On Sun, 16 Apr 2006 20:52:54 -0700, "Jim Hughes"
[quoted text, click to view]

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
dgk
4/18/2006 8:24:08 AM
On 17 Apr 2006 07:30:47 -0700, "Hugh Janus"
[quoted text, click to view]

I just wanted to know if a text string would be a valid filename, not
dgk
4/18/2006 8:26:56 AM
On Tue, 18 Apr 2006 13:11:03 +0100, "Andrew Morton"
[quoted text, click to view]

Thanks. It isn't as easy as the old days where the QuickPak
Professional library just had a ValidFilename function. Where is Ethan
Andrew Morton
4/18/2006 1:11:03 PM
[quoted text, click to view]

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

Barry
4/18/2006 11:20:17 PM
On Wed, 19 Apr 2006 00:14:16 GMT, "Michael D. Ober"
[quoted text, click to view]
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

[quoted text, click to view]

Michael D. Ober
4/19/2006 12:14:16 AM
Take a look at system.io.path. There are several member functions that
return path seperators, valid characters, etc.

Mike Ober.

[quoted text, click to view]


AddThis Social Bookmark Button