|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Is there a Filename.IsValid function anywhere?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? ..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? On Sun, 16 Apr 2006 16:05:34 -0700, "Jim Hughes"
<NOSPAMJ3033@Hotmail.com> wrote: >.NET Framework Class Library Thanks, that makes things easier. But all it returns is "<>| those are>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 > > the only invalid file characters? / and \ should be illegal. What about ? and * #%~` Well, I guess that they're legal.
Show quote
Hide quote
"dgk" <NoWh***@MailsAnonymous.com> wrote in message If all you did is a System.IO.Path.GetInvalidFileNameChars().ToString(), I 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. 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). 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 I just hovered over the variable that I used and looked at the text>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). > > > 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> 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. File.Exists(Filename) ???? On 17 Apr 2006 07:30:47 -0700, "Hugh Janus"
<my-junk-acco***@hotmail.com> wrote: >> I just wanted to know if a text string would be a valid filename, not>> 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) ???? whether it exists. 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. > 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 I've found that System.IO.Path.GetFullPath() will throw an exception>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. >> 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>
Show quote
Hide quote
"dgk" <NoWh***@MailsAnonymous.com> schrieb: The "Remarks" section is saying "The array returned from this method is not >>.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. 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" <sonicechoes@zero-spam-hotmail.com> schrieb: I think that's a clean solution.>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. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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: It really works well. Trying to open a streamreader gives an "illegal>>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. characters" error. If the name is valid it causes a "not found" exception. Unless, I suppose, the file actually exists. So be it. dgk wrote:
> I can't find anything in the framework that will tell me whether a See > 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: 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 On Tue, 18 Apr 2006 13:11:03 +0100, "Andrew Morton"
<a**@in-press.co.uk.invalid> wrote: >dgk wrote: Thanks. It isn't as easy as the old days where the QuickPak>> 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 > Professional library just had a ValidFilename function. Where is Ethan Winer when I need him? |
|||||||||||||||||||||||