|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Valid file nameIs 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 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 Adamz5 wrote:
Show quoteHide quote > Hi Jack, I tried file exists (before posting) with an invalid name - a/b.txt and > > 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 > > it did not throw an exception (much to my surprise) Jack 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 This would report an invalid path even if the filename is syntactically
correct but just doesn't exist! Simon -- Show quoteHide quote================================ Simon Verona Dealer Management Service Ltd Stewart House Centurion Business Park Julian Way Sheffield S9 1GD Tel: 0870 080 2300 Fax: 0870 735 0011 "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 > 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 Adamz5 wrote:
Show quoteHide quote > Okay here you go... Yes that is basically what I did but knowing MS there are other things > 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 > 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 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 > 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 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 Claes Bergefall wrote:
Show quoteHide quote > Try Path.GetFileName or Path.GetDirectoryName (or both). Both will throw an Thanks for that> 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 > > > 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 > > > 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 "Göran Andersson" <gu***@guffa.com> schrieb: ACK, but note that this won't guarantee the validity of the filename because > Use the array of invalid characters from the Path class: > > If fileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) <> -1 Then '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/> Göran Andersson wrote:
> Use the array of invalid characters from the Path class: This would qualify:> > If fileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) <> -1 Then > > "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
timer control
TreeView control checked based on if records exist What exactly does it mean when they say CreateInstance and late binding what is best practice to populate large combobox? EM_CHARFROMPOS with RichTextBox Validating Data Using a Error Provider DBNull problem VB 2005 Newbie - Binding? Data binding to an object |
|||||||||||||||||||||||