0

I got a String which could be : C:\Users\info\Desktop\Folder in which case I want to replace the Part of the URL after \Users\ ..\ with %Username%.

So far I manage to replace the string when i know the part before the Username, in this case Users\ and the part after the Username. But i can't manage to define \ as the end because it thinks that Users \ is the end of string to replace.

So how can i change the dim SDelimEnd so it just gives back the Username

Public Class Form1
Private Sub RadButton_Click(sender As Object, e As EventArgs) Handles RadButton.Click
Dim sSource As String = txt1.text 'String that is being searched
Dim sDelimStart As String = "Users" 'First delimiting word
Dim sDelimEnd As String = "AppData" 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
    Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
    MessageBox.Show(res) 'Display
        Strings.Replace(sSource,res,"\%username%\")
        MessageBox.Show(Strings.Replace(sSource,res,"\%username%\"))
        txt2.Text = Strings.Replace(sSource,res,"\%username%\")

Else
    MessageBox.Show("One or both of the delimiting words were not found!")
End If

End Sub

End Class

Daniel Lenzendorf
  • 97
  • 1
  • 2
  • 11
  • There is an official way in Win32 to get the profile directory path of a specified user - because users can have profile directories in different locations, or be using a roaming profile, or have a profile directory name that's different than their actual Windows username, and so on. Are you certain this is how you want to go about it - by doing a naive string-replacement? – Dai May 21 '17 at 10:14
  • Yes for the first. Or do you have a better work arround? :) Thank you for your answer – Daniel Lenzendorf May 21 '17 at 10:26
  • See here: http://stackoverflow.com/questions/198124/how-can-i-get-the-path-of-a-windows-special-folder-for-a-specific-user – Dai May 21 '17 at 10:32
  • `String.Contains` to check if string has **Users** inside it. Then `IndexOf("Users") + 6` to get everything after **Users\** and just replace – Mederic May 21 '17 at 10:47
  • The Username could have any length – Daniel Lenzendorf May 21 '17 at 10:58
  • if its windows user yuo can retrieve it with: `Environment.UserName` – Mederic May 21 '17 at 11:50

3 Answers3

1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If System.IO.Directory.Exists("C:\Users\info\" + TextBox1.Text) = True Then
        Label1.Text = "Ok"
    Else
        Label1.Text = "No"
    End If
End Sub
Manu
  • 11
  • 2
0

Declare your url with myurl and apply this code:

If myurl.contains("Users") Then
    myurl = myurl.Replace("info\Desktop\Folder", "%Username%")
End If

If myurl can be longer path (or different than info\Desktop\Folder, eg: info\Desktop\Folder\hello\wazapp) then:

If myurl.contains("Users") Then
    Dim cut_at As String = "\Users\"
    Dim stringSeparators() As String = {cut_at}
    Dim split = original.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
    myurl = myurl.replace(split(1), "")
End If
Stefan
  • 250
  • 1
  • 2
  • 9
0

I found the Solution but I can not define Users\ as Start and \ as end.

Public Class Form1
Private Sub RadButton_Click(sender As Object, e As EventArgs) Handles RadButton.Click
Dim sSource As String = txt1.text 'String that is being searched
Dim sDelimStart As String = "Users" 'First delimiting word
Dim sDelimEnd As String = "AppData" 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
    Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
    MessageBox.Show(res) 'Display
        Strings.Replace(sSource,res,"\%username%\")
        MessageBox.Show(Strings.Replace(sSource,res,"\%username%\"))


Else
    MessageBox.Show("One or both of the delimiting words were not found!")
End If

End Sub

End Class

Daniel Lenzendorf
  • 97
  • 1
  • 2
  • 11