2016-12-15 65 views
0

在我的代码中,我必须使用通配符“*”来搜索一个类似文件夹的名称。这段代码在VBA中工作得很好(Process.Start ...行除外),但现在它只是打开“我的文档”。VB.net文件夹路径不工作

它应该打开一个网络文件夹。

字符串pathStr产生最终的文件夹路径,这是正确的

我已经三检查文件夹路径(我复制这段代码的产生,并在Windows资源管理器中粘贴和路径检查出来)

为什么我的代码只打开我的文档?

Private Sub OpenJobToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenJobToolStripMenuItem.Click 
    Dim jobnum As String = ListView1.SelectedItems(0).Text 

    'Define the master path to all job numbers 
    Dim masterpath As String = "\\ussatf02\Production\00A Job Folders\" 

    'Get the child folder path 
    Dim fullFolder As String = masterpath & jobnum.Substring(0, 5) & "xxx\" 

    'Get first 8 characters of job number 
    Dim jobFolder As String = jobnum.Substring(0, 8) 

    'Define the full path to the jobFolder 
    Dim xPath As String = fullFolder & jobFolder 

    'Check the full path with a wildcard to see if there is a folder named something simliar to what we have 
    Dim foundFolder As String = Dir(xPath & "*", vbDirectory) 

    'If the folder is not found (length < 2) then throw an error, else open the folder 
    If (foundFolder.Length < 2) Then 
     Dim msgRes As MsgBoxResult = MsgBox("The job folder for: " & jobnum & " could not be found.", vbCritical, "Error Finding Folder") 
    Else 

     'Define the final path of the folder 
     Dim pathStr As String = xPath & "\" & foundFolder 

     'Open the folder 
     System.Diagnostics.Process.Start("explorer.exe", pathStr) 
    End If 

End Sub 

任何帮助将不胜感激!

编辑

我现在取代了线

Dim pathStr As String = xPath & "\" & foundFolder 

对于

Dim pathStr As String = System.IO.Path.GetDirectoryName(xPath & "\" & foundFolder) 

我仍然得到同样的结果

+0

您为xPath获得了什么价值? –

+0

@TonyDong我得到:'\\ ussatf02 \ Production \ 00A Job Folders \ 21643xxx \ 21643043'。问题是有些员工会在文件夹末尾添加额外的字符(例如,重命名文件夹:21643043 - 我的客户) - 所以我必须发出通配符搜索。 – Sanya

+0

你能找到foundFolder吗? –

回答

1

Explorer将转到一个默认的文件夹中这种情况下'我的文档',如果你是文件夹试图打开不在那里。确保pathStr存在。

愿你的文件夹,包括Unicode字符,看到更多的在这个网址C#: System.Diagnostics.Process.Start("Explorer.exe", @"/select" + FilePath). Can not open file when file's name is unicode character

System.Diagnostics.Process.Start( “Explorer.exe中”, “/选择”, “” & pathStr & “”, “”)

+0

还是什么都没有。我必须在引号内移动“@”(Intellisense不喜欢它在双引号之外)。我也尝试用'+'代替'&'。我正在使用VB.net,所以也许这就是为什么它不同... – Sanya

+1

对不起,我忘了,@没有在VB.net工作,请参阅更新代码 –