figuring out chars in a string (parsing)

This should be a simple question to answer to the right person. I have been trying to figure out a way to do the foolowing but can't seem to come up with it.

I have a string that will change (directory path). I want to know if the folder (at the end of the string is 'Desktop' even if it is 'Desktop\' How can I do this if I have the file path

I figure i need something to get the variables on the right of the string somehow. Any help would be appreciated. Thanks!



Answer this question

figuring out chars in a string (parsing)

  • Eric Bickle - BlueRealm

    I was unaware of that method.. I like it very much. I think I will revert to this solution. Thanks!
  • nate-d-o-double-g

    I'd suggest something like this:

     

    OpenFileDialog1.ShowDialog()     'This is arbitrary, I used it to test the code

    Dim FileString As String = OpenFileDialog1.FileName
    Dim Length As Integer = 0
    Dim Index As Integer = 0
    Dim IsDesktop As Boolean = False

    'Loop to find the position of the last directory identifier "\"
    Do Until Index = -1
         Index = FileString.IndexOf(
    "\")
         FileString = FileString.Substring(Index + 1)
         Length += Index + 1
    Loop

    'This gets the string that is just the directory (everything up to the last "\")
    FileString = OpenFileDialog1.FileName.Substring(0, Length - 1)

    'Here is where you run the comparison
    IsDesktop = My.Computer.FileSystem.SpecialDirectories.Desktop.Equals(FileString, System.StringComparison.CurrentCultureIgnoreCase)



  • dmaietta

    This is helpful but not exactly what I was looking for in my situation. I ended up using the following solution. I appreciate your help!

    Dim newstring As String = pathoffolder.Substring(pathoffolder.Length - 7)

    if newstring = "Desktop" then

    exit sub

    end if


  • Sapna

    Well the pathoffolder isnt an object, it is a string. For example

    dim pathoffolder = "C:\Documents and Settings\user\my documents"

    Dim newstring As String = pathoffolder.Substring(pathoffolder.Length - 7)

    'newstring will return a value of "cuments"

    Make sense


  • ToToX

    Although the code may work for your scenerio, you should use the .Net methods provided for you to find the directory name of a path:

    Dim MyPath As String = "C:\DataDrive\Graphics"

    Dim di As New DirectoryInfo(MyPath)

    If di.Name = "Graphics" Then

    MessageBox.Show(di.FullName)

    End If

    the "Name" method of the DirectoryInfo class will return the directory name....fullname returns the full path



  • bryanedds

    I'm not sure exactly how that works, but I'd love to know exactly what the "pathoffolder" object you're using is.

  • figuring out chars in a string (parsing)