0

In my code I have to use a wildcard "*" to search for a folder name that is simliar. This code works just fine in VBA (except for the Process.Start ... line), but all it does now is open My Documents.

It is supposed to open a network folder.

The string pathStr produces the final folder path, and that is correct

I have triple-checked the folder path (I copied what this code produces and pasted in Windows Explorer and the path checks out)

Why is my code only opening My Documents?

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

Any help will be greatly appreciated!!

EDIT

I now substituted the line

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

For

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

And I still get the same result

Sanya
  • 1,270
  • 5
  • 21
  • 47
  • What value did you get for xPath? – Tony Dong Dec 15 '16 at 22:19
  • @TonyDong I get: `\\ussatf02\Production\00A Job Folders\21643xxx\21643043`. The problem is that some employees will add additional characters to the end of the folder (example, rename the folder: 21643043 - My customer) - so I have to issue a wildcard search. – Sanya Dec 15 '16 at 22:24
  • Can you get foundFolder? – Tony Dong Dec 15 '16 at 22:26
  • 1
    Could it not be an authentication thing? I encountered this issue in the past myself where Windows would push me out to My Documents on the local computer rather than a network path. I found that it was because I had no authentication to the network path and windows wasn't prompting me for credentials. – Riples Dec 15 '16 at 22:26
  • @TonyDong Yes, all paths produce valid strings (I check the paths and they are correct) – Sanya Dec 15 '16 at 22:31
  • @Riples I think you may be on to something here. Thank you – Sanya Dec 15 '16 at 22:32

1 Answers1

1

Explorer will go to a default folder in this case 'My Documents', if the folder you are attempting to open is not there. Make sure pathStr exists.

May your folder include Unicode character, see more in this URL 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", "/select,""" & pathStr & """")

Community
  • 1
  • 1
Tony Dong
  • 3,213
  • 1
  • 29
  • 32
  • Still nothing. I had to move the "@" inside the quotes (Intellisense did not like it outside the double-quotes). I also tried substituting `+` for `&`. I am using VB.net so maybe this is why its different... – Sanya Dec 15 '16 at 23:13
  • 1
    Sorry, I forgot, @ did not working in VB.net, please see update code – Tony Dong Dec 15 '16 at 23:25