我有一个工具可以读取.txt文件以获取路径列表并在副本中使用它们。它看起来像这样:
***DESTINATION***
E:\Backup
***Sources***
%USERPROFILE%\Pictures
%USERPROFILE%\Favourites
%USERPROFILE%\Contacts
%USERPROFILE%\My Videos
我的系统启用了文件夹重定向,因此“图片”实际上是D:\ Adam \ Pictures。但是,当使用以下代码时,它将仅解析为C:\ Adam \ Pictures并抛出“无法找到路径错误”。
'Declarations earlier in script
Dim Destpath As String = System.IO.File.ReadAllLines(Application.StartupPath + "\CONFIG.txt")(1)
Dim userprofilevar = (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
'Snippet of line reading logic
ElseIf line.Contains("%USERPROFILE%") Then
Dim lineArray() As String = line.Split("\")
Dim lineuser As String = userprofilevar + "\"
Dim linepath As String = lineArray(1)
line = lineuser + linepath
Destpath = String.Concat(Destpath, "\", linepath)
MessageBox.Show(Destpath)
Else
有没有人知道如何通过%USERPROFILE%变量解析重定向文件夹的正确路径?
答案 0 :(得分:0)
解决方案我发现:
将@ AlexB的示例转换为VB.NET并使其在我的应用程序中运行后,我只是将每个%Userprofile%
文件夹声明为字符串然后让行读取逻辑更新{{1 } string作为正确的文件夹路径:
line
对于所有11个'Declarations as start of Form
Dim downloadsPath As String = KnownFolders.GetPath(KnownFolder.Downloads)
Dim contactspath As String = KnownFolders.GetPath(KnownFolder.Contacts)
'Snippit of line reading logic
ElseIf line.Contains("%USERPROFILE%\Downloads") Then
line = downloadspath
ElseIf line.Contains("%USERPROFILE%\Contacts") Then
line = contactspath
文件夹,它并不漂亮,但它有效!