Vb.net 更改URL的一部分-将用户替换为%USERNAME%

Vb.net 更改URL的一部分-将用户替换为%USERNAME%,vb.net,Vb.net,我得到一个字符串,可能是:C:\Users\info\Desktop\Folder,在这种情况下,我想用%Username%替换\Users\..\之后的URL部分 到目前为止,当我知道用户名前面的部分时,我成功地替换了字符串,在本例中是Users\和用户名后面的部分。但我无法将\定义为end,因为它认为Users\是要替换的字符串的末尾 那么我如何更改dim SDelimEnd,使其只返回用户名呢 Public Class Form1 Private Sub RadButton_Click(s

我得到一个字符串,可能是:C:\Users\info\Desktop\Folder,在这种情况下,我想用%Username%替换\Users\..\之后的URL部分

到目前为止,当我知道用户名前面的部分时,我成功地替换了字符串,在本例中是Users\和用户名后面的部分。但我无法将\定义为end,因为它认为Users\是要替换的字符串的末尾

那么我如何更改dim SDelimEnd,使其只返回用户名呢

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
结束类

使用myurl声明您的url并应用以下代码:

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
If myurl.contains("Users") Then
    myurl = myurl.Replace("info\Desktop\Folder", "%Username%")
End If
如果myurl的路径较长或与info\Desktop\Folder不同,例如:info\Desktop\Folder\hello\wazapp,则:


我找到了解决方案,但无法将用户\定义为开始和\结束

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

在Win32中,有一种官方方法可以获取指定用户的配置文件目录路径,因为用户可以在不同的位置拥有配置文件目录,或者使用漫游配置文件,或者配置文件目录名称与实际的Windows用户名不同,等等。你确定这就是你想要的方式吗?通过简单的字符串替换吗?第一个是。或者你周围有更好的工作吗感谢您的回答请参见此处:String.Contains以检查字符串中是否有用户。然后IndexOfUsers+6在*Users*之后获取所有内容,并仅替换用户名。用户名可能有任何纵向影响,这在用户名为Daniel或ABC92ue2u时也有效?是的­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­我尝试了这段代码
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