locked
My.Computer.FileSystem.GetFiles does not find all files RRS feed

  • Question

  • I'm currently working on a program, and it needs to find desktops .lnk and .exe files. At this moment, it finds them, but not all.

    Here's bit of code, where the problem seems to be.

     For Each foundFile As String In My.Computer.FileSystem.GetFiles(
      My.Computer.FileSystem.SpecialDirectories.Desktop, SearchOption.SearchTopLevelOnly, "*")
    
                If IO.Path.GetExtension(foundFile).ToLower = ".lnk" Then
                    If IO.Path.GetExtension(GetTargetPath(foundFile)).ToLower = ".exe" Then
    
                        ListBox1.Items.Add(GetTargetPath(foundFile) & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
    
    
                    End If
    
    
                ElseIf IO.Path.GetExtension(foundFile).ToLower = ".exe" Then
                    ListBox1.Items.Add(foundFile & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
                End If
            Next

    This is all what it finds:

    http://i.imgur.com/nYmcWUe.png

    (it resolves .lnk's to exe url, eg. Office16 items were originally in .lnk)

    There should be for example WinSCP which has also .lnk

    http://i.imgur.com/Tehx2wN.png

    BUT: when I removed WinSCP from desktop and bringed it back again, it showed up in list?!?

    there's much more programs similar to WinSCP, that do not show up in my list


    • Edited by P0t4t0 Monday, July 11, 2016 7:05 PM
    Monday, July 11, 2016 7:04 PM

Answers

  •  This is because the Desktop directory gives you a path to the Desktop folder in your user folder. For example, 

    C:\Users\YourUserName\Desktop

     However,  the WinSCP shortcut is actually placed in the Public Desktop folder (path shown below) when you install it.

    C:\Users\Public\Desktop

     So in short, you will see it on your desktop but,  it is not actually located in your desktop folder,  the one you are getting the .lnk files from.  So,  if you are using a newer version of Visual Studio that will allow you to target (.Net Framework 4.0) or above you can get the shortcuts from that directory too by using the CommonDesktopDirectory.

    I changed your code around a little to add the files to the ListBox inside a Sub.  Then you can easily call that sub twice,  once with the Desktop directory and the second time with the CommonDesktop directory. That will list all the files.  Give it a try in a new Form project with 1 Button and 1 ListBox added to the form.  You need to add a reference to (Windows Script Host Object Model) as explained at the top of the code.  I assume you know that though.

    Imports IWshRuntimeLibrary 'add a reference to (Windows Script Host Object Model) in the Com tab of the Add Reference dialog window
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListFilesFrom(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) 'list them from your desktop folder
            ListFilesFrom(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)) 'list the files from the public desktop folder
        End Sub
    
        Private Sub ListFilesFrom(ByVal FolderPath As String)
            For Each foundFile As String In IO.Directory.GetFiles(FolderPath)
                Dim ext As String = IO.Path.GetExtension(foundFile).ToLower
                If ext = ".lnk" Then
                    If IO.Path.GetExtension(GetTargetPath(foundFile)).ToLower = ".exe" Then
                        ListBox1.Items.Add(GetTargetPath(foundFile) & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
                    End If
                ElseIf ext = ".exe" Then
                    ListBox1.Items.Add(foundFile & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
                End If
            Next
        End Sub
    
        Private Function GetTargetPath(ByVal ShortcutFilename As String) As String
            Dim theShell As New WshShell()
            Dim theShortcut As WshShortcut = CType(theShell.CreateShortcut(ShortcutFilename), WshShortcut)
            Return theShortcut.TargetPath
        End Function
    End Class
     

     "BUT: when I removed WinSCP from desktop and bringed it back again, it showed up in list?!?  there's much more programs similar to WinSCP, that do not show up in my list"

     If you delete the Shortcut and then create another on for that exe on the desktop,  it will be added to your Desktop folder instead of the Public Desktop folder.  This may very well be where the rest of your shortcuts are probably at too.

     


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Monday, July 11, 2016 9:59 PM
    • Edited by IronRazerz Monday, July 11, 2016 11:58 PM
    • Marked as answer by P0t4t0 Thursday, July 14, 2016 7:49 PM
    Monday, July 11, 2016 9:54 PM

All replies

  • P0,

    You can use a string array as the file filter so that you can include .url also. That way you don't need that extra stuff to test the file extension.


    In the middle of difficulty ... lies opportunity. -- Albert Einstein

    Monday, July 11, 2016 7:12 PM
  • P0,

    Try this and see how it fares for you:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load Dim filePaths As IEnumerable(Of String) = _ GetShortcutsFromDesktop() Stop End Sub Private Function _ GetShortcutsFromDesktop(Optional ByVal fileFilter() As String = Nothing) As IEnumerable(Of String) Dim retVal As IEnumerable(Of String) = Nothing Try Dim filter() As String = Nothing If fileFilter IsNot Nothing AndAlso fileFilter.Length > 0 Then filter = fileFilter Else filter = New String() {"*.lnk", "*.url"} End If Dim filesList As New List(Of String) Dim desktop As String = My.Computer.FileSystem.SpecialDirectories.Desktop For Each s As String In filter Dim files As IEnumerable(Of String) = IO.Directory.EnumerateFiles(desktop, s) If files IsNot Nothing AndAlso files.Count > 0 Then filesList.AddRange(files) End If Next If filesList.Count > 0 Then filesList.Sort() retVal = filesList.ToArray End If Catch ex As Exception MessageBox.Show(String.Format("An error occurred:{0}{0}{1}", _ vbCrLf, ex.Message), _ "Exception Thrown", _ MessageBoxButtons.OK, _ MessageBoxIcon.Warning) End Try Return retVal End Function End Class



    In the middle of difficulty ... lies opportunity. -- Albert Einstein

    Monday, July 11, 2016 7:31 PM
  •  This is because the Desktop directory gives you a path to the Desktop folder in your user folder. For example, 

    C:\Users\YourUserName\Desktop

     However,  the WinSCP shortcut is actually placed in the Public Desktop folder (path shown below) when you install it.

    C:\Users\Public\Desktop

     So in short, you will see it on your desktop but,  it is not actually located in your desktop folder,  the one you are getting the .lnk files from.  So,  if you are using a newer version of Visual Studio that will allow you to target (.Net Framework 4.0) or above you can get the shortcuts from that directory too by using the CommonDesktopDirectory.

    I changed your code around a little to add the files to the ListBox inside a Sub.  Then you can easily call that sub twice,  once with the Desktop directory and the second time with the CommonDesktop directory. That will list all the files.  Give it a try in a new Form project with 1 Button and 1 ListBox added to the form.  You need to add a reference to (Windows Script Host Object Model) as explained at the top of the code.  I assume you know that though.

    Imports IWshRuntimeLibrary 'add a reference to (Windows Script Host Object Model) in the Com tab of the Add Reference dialog window
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListFilesFrom(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) 'list them from your desktop folder
            ListFilesFrom(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)) 'list the files from the public desktop folder
        End Sub
    
        Private Sub ListFilesFrom(ByVal FolderPath As String)
            For Each foundFile As String In IO.Directory.GetFiles(FolderPath)
                Dim ext As String = IO.Path.GetExtension(foundFile).ToLower
                If ext = ".lnk" Then
                    If IO.Path.GetExtension(GetTargetPath(foundFile)).ToLower = ".exe" Then
                        ListBox1.Items.Add(GetTargetPath(foundFile) & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
                    End If
                ElseIf ext = ".exe" Then
                    ListBox1.Items.Add(foundFile & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
                End If
            Next
        End Sub
    
        Private Function GetTargetPath(ByVal ShortcutFilename As String) As String
            Dim theShell As New WshShell()
            Dim theShortcut As WshShortcut = CType(theShell.CreateShortcut(ShortcutFilename), WshShortcut)
            Return theShortcut.TargetPath
        End Function
    End Class
     

     "BUT: when I removed WinSCP from desktop and bringed it back again, it showed up in list?!?  there's much more programs similar to WinSCP, that do not show up in my list"

     If you delete the Shortcut and then create another on for that exe on the desktop,  it will be added to your Desktop folder instead of the Public Desktop folder.  This may very well be where the rest of your shortcuts are probably at too.

     


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Monday, July 11, 2016 9:59 PM
    • Edited by IronRazerz Monday, July 11, 2016 11:58 PM
    • Marked as answer by P0t4t0 Thursday, July 14, 2016 7:49 PM
    Monday, July 11, 2016 9:54 PM
  • IR,

    I'm not familiar with the new stuff so you're probably right.


    In the middle of difficulty ... lies opportunity. -- Albert Einstein

    Monday, July 11, 2016 9:59 PM
  • IR,

    I'm not familiar with the new stuff so you're probably right.


    In the middle of difficulty ... lies opportunity. -- Albert Einstein

     I have that program installed and it baffled me for a second but,  as soon as i looked in my user\desktop folder and did not see the shortcut for it there,  i knew what the problem was.

     I don`t have it installed on my older XP computer so,  i`m not sure where it would place the shortcut on there.  8)


    If you say it can`t be done then i`ll try it

    Monday, July 11, 2016 10:08 PM
  • I'm currently working on a program, and it needs to find desktops .lnk and .exe files. At this moment, it finds them, but not all.

    Here's bit of code, where the problem seems to be.

     For Each foundFile As String In My.Computer.FileSystem.GetFiles(
      My.Computer.FileSystem.SpecialDirectories.Desktop, SearchOption.SearchTopLevelOnly, "*")
    
                If IO.Path.GetExtension(foundFile).ToLower = ".lnk" Then
                    If IO.Path.GetExtension(GetTargetPath(foundFile)).ToLower = ".exe" Then
    
                        ListBox1.Items.Add(GetTargetPath(foundFile) & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
    
    
                    End If
    
    
                ElseIf IO.Path.GetExtension(foundFile).ToLower = ".exe" Then
                    ListBox1.Items.Add(foundFile & "*|*" & IO.Path.GetFileNameWithoutExtension(foundFile))
                End If
            Next

    This is all what it finds:

    http://i.imgur.com/nYmcWUe.png

    (it resolves .lnk's to exe url, eg. Office16 items were originally in .lnk)

    There should be for example WinSCP which has also .lnk

    http://i.imgur.com/Tehx2wN.png

    BUT: when I removed WinSCP from desktop and bringed it back again, it showed up in list?!?

    there's much more programs similar to WinSCP, that do not show up in my list



    As for your actual issue, "My.Computer.FileSystem.GetFiles does not find all files", disregarding the desktop GetFiles can be disallowed from accessing various folders, for various users including admin users, on a system depending on the settings for the folders properties. Even then I believe it can be disallowed from retrieving file information in an allowed folder if the files properties disallow your user, even with admin priviliges, to access the file(s) in question.

    La vida loca

    Monday, July 11, 2016 11:45 PM