Results 1 to 9 of 9

Thread: Check if a file exist in directory/subfolders and show its Explorer windows folder

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Check if a file exist in directory/subfolders and show its Explorer windows folder

    I'm using this code in order to check if file exist. Unfortunately It doesn't search also in subdirectory.
    Code is:
    Code:
     Private Async Function ParentMethod() As Task
            Dim filePath As String = Await Task.Run(
            Function()
    
                Return Directory.EnumerateFiles(My.Settings.Cartellasalvataggio, titolo & ".mp3",
                              SearchOption.AllDirectories).FirstOrDefault()
            End Function)
            If Not String.IsNullOrEmpty(filePath) Then
                LinkLabel1.Text = "File exist already"
                LinkLabel1.Visible = True
                PictureBox7.Visible = True
            Else
                MsgBox("It doesn't exist. ")
    
            End If
    
        End Function
    and the following class

    Code:
    Imports System.IO
    Imports System.Runtime.InteropServices
    
    Public Class NativeMethods
        <DllImport("shell32.dll", SetLastError:=True)>
        Private Shared Function SHOpenFolderAndSelectItems(
                pidlFolder As IntPtr, cidl As UInteger,
                <[In], MarshalAs(UnmanagedType.LPArray)> apidl As IntPtr(),
                dwFlags As UInteger) As Integer
        End Function
    
        <DllImport("shell32.dll", SetLastError:=True)>
        Private Shared Sub SHParseDisplayName(
                <MarshalAs(UnmanagedType.LPWStr)> name As String,
                bindingContext As IntPtr, <Out> ByRef pidl As IntPtr,
                sfgaoIn As UInteger, <Out> ByRef psfgaoOut As UInteger)
        End Sub
    
        Public Shared Sub OpenFolderAndSelectFile(filePath As String)
            Dim dirPath As String = Path.GetDirectoryName(filePath)
            Dim fileName As String = Path.GetFileName(filePath)
            OpenFolderAndSelectFile(dirPath, fileName)
        End Sub
    
        Public Shared Sub OpenFolderAndSelectFile(dirPath As String, fileName As String)
            Dim nativeFolder As IntPtr
            Dim psfgaoOut As UInteger
            SHParseDisplayName(dirPath, IntPtr.Zero, nativeFolder, 0, psfgaoOut)
    
            If nativeFolder = IntPtr.Zero Then
                ' Log error, can't find folder
                Return
            End If
    
            Dim nativeFile As IntPtr
            SHParseDisplayName(Path.Combine(dirPath, fileName),
                               IntPtr.Zero, nativeFile, 0, psfgaoOut)
    
            Dim fileArray As IntPtr()
            If nativeFile = IntPtr.Zero Then
                ' Open the folder without the file selected if we can't find the file
                fileArray = New IntPtr(-1) {}
            Else
                fileArray = New IntPtr() {nativeFile}
            End If
    
            SHOpenFolderAndSelectItems(nativeFolder, CUInt(fileArray.Length), fileArray, 0)
    
            Marshal.FreeCoTaskMem(nativeFolder)
            If nativeFile <> IntPtr.Zero Then
                Marshal.FreeCoTaskMem(nativeFile)
            End If
        End Sub
    End Class
    Then I am calling it with

    Code:
    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
    
            NativeMethods.OpenFolderAndSelectFile((IO.Path.Combine(My.Settings.Cartellasalvataggio, titolo & ".mp3"))
        End Sub
    The problems I have are two:

    - 1) The code doesn't search in subdirectory
    - 2) The linklabel is updated just If I call the class ParentMethod two times.. Why?


    Hope I explained the situation clearly.
    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    I tried this... It works

    Code:
    Imports System.IO
    
    Public Class Form1
    
        Dim titolo As String = "3D pie test"
        Dim ext As String = ".suo"
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            My.Settings.Cartellasalvataggio = "Z:\Documents\Visual Studio 2008\"
        End Sub
    
        Private Async Function ParentMethod() As Task
            Dim filePath As String = Await Task.Run(
            Function()
    
                Return Directory.EnumerateFiles(My.Settings.Cartellasalvataggio, titolo & ext,
                              SearchOption.AllDirectories).FirstOrDefault()
            End Function)
            If Not String.IsNullOrEmpty(filePath) Then
                Process.Start("explorer.exe", "/select," & filePath)
            Else
                MsgBox("It doesn't exist. ")
    
            End If
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ParentMethod()
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    Hi .paul. Thanks for your time helping me.
    So, your code is basically mine, but you use process start. I'm usint that class because process start is freezing the explorer window and it got some limitation too!
    So, I'm choosing that class, but both codes( mine and yours) are not finding a file in subdirectory.
    SO for example if you are searching for a file that is in Desktop/music it can't find it. Actually, it is saying "file found" but when I am going to click on linklabel, it brings me in the folder of Explorer file(windows explorer)..

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    I haven’t used a LinkLabel for years, so I can’t advise on that, but my code found a file several directories deeper and opened explorer with the correct file selected on the first click...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    Quote Originally Posted by .paul. View Post
    I haven’t used a LinkLabel for years, so I can’t advise on that, but my code found a file several directories deeper and opened explorer with the correct file selected on the first click...
    What do you mean with that? Should I avoid LinkLabel? I don't thinkLinkLabel is responsible in this case.
    I was using this code:
    Code:
    file = Directory.GetFiles(filepath,Filename,
                              SearchOption.AllDirectories).FirstOrDefault()
    
       Process.Start("explorer.exe", "/select," & file.ToString)
    and this was doing the job about finding the file in subdirectory, but process start was freezing the explorer window for about 4 seconds.
    Then I came through this class and It is fixing the freezing issue, but subdirectory files are not found.
    I don't know either if you are using or tried that class, but there is something in there that is broken.
    Thanks Anyway for your kind help

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    Looking at it again, it seems you need to declare filePath at form scope, and change this...

    Code:
    NativeMethods.OpenFolderAndSelectFile((IO.Path.Combine(My.Settings.Cartellasalvataggio, titolo & ".mp3"))
    To this...



    Code:
    NativeMethods.OpenFolderAndSelectFile(filePath)

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    I’m not suggesting you don’t use a LinkLabel

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    Quote Originally Posted by .paul. View Post
    Looking at it again, it seems you need to declare filePath at form scope, and change this...

    Code:
    NativeMethods.OpenFolderAndSelectFile((IO.Path.Combine(My.Settings.Cartellasalvataggio, titolo & ".mp3"))
    To this...



    Code:
    NativeMethods.OpenFolderAndSelectFile(filePath)
    This was exactly the answer! How did you figured it out? Thanks!

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check if a file exist in directory/subfolders and show its Explorer windows folde

    Quote Originally Posted by matty95srk View Post
    This was exactly the answer! How did you figured it out? Thanks!
    I should’ve spotted it sooner. I was distracted by all of the API functions you’re using

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width