Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2011
    Messaggi
    37

    [VB .NET 2010] Aprire una finestra esplora risorse con 2 o più file selezionati

    Salve a tutti!!!

    Come da titolo nel mio programma vb .net volevo far si che si aprisse una finestra explorer con selezionati (evidenziati) più file.

    Nel caso in cui il file sia uno ho adottato questo codice:

    Process.Start("explorer.exe", " /select ," file_da_selezionare)

    E fin qui va bene, ma se devo selezionarne 2?
    Ovviamente posto che la cartella e i files esistono, come posso far aprire la cartella e far selezionare più file?
    Ho provato con SHOpenFolderAndSelectItems ma al momento non riesco a farlo funzionare.

    L'ho impostato così, magari trovate l'errore:

    dichiarazione
    codice:
    Public Shared Function SHOpenFolderAndSelectItems(ByVal pidlFolder As IntPtr, ByVal cidl As UInteger, <MarshalAs(UnmanagedType.LPArray)> ByVal apidl() As IntPtr, ByVal dwFlags As UInteger) As Integer
    dim cartella as string ="path cartella da aprire"
    dim cartellapunt as intptr = Marshal.StringToBSTR(cartella)
    dim file as string() ="files da selezionare"
    dim filepunt as intptr () =Marshal.StringToBSTR(file (posizione))

    SHOpenFolderAndSelectItems(cartellapunt, filepunt.Count, filepunt, 0)

    Cos'è che non funziona??

    Ho trovato anche questa discussione, è in c++ e non riesco a trovare alcuni corrispettivi di codice, magari qualcuno può aiutarmi:
    Programatically select multiple files in windows explorer

    Qualcuno sa darmi una mano a riguardo?

    Grazie mille!

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2011
    Messaggi
    37
    Nessuno?

    Soprattutto io non riesco a capire come dichiarare e poi creare i parametri:

    pidlFolder [in]
    Type: PCIDLIST_ABSOLUTE

    A pointer to a fully-qualified item ID list that specifies the folder.

    apidl [in, optional]
    Type: PCUITEMID_CHILD_ARRAY

    A pointer to an array of PIDL structures, each of which is an item to select in the target folder referenced by pidlFolder.

    MSDN

  3. #3
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,463
    A mio avviso, ti conviene ricercare il formato dei parametri da passare all'eseguibile explorer.exe, da invocare tramite Process.Start, affinché si posizioni automaticamente nella cartella desiderata e selezioni gli elementi richiesti.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2011
    Messaggi
    37
    Originariamente inviato da alka
    A mio avviso, ti conviene ricercare il formato dei parametri da passare all'eseguibile explorer.exe, da invocare tramite Process.Start, affinché si posizioni automaticamente nella cartella desiderata e selezioni gli elementi richiesti.
    Qualche riferimento a quello che dici?

    Perché come dicevo nel primo post ho trovato per l'appunto i parametri da passare ad explorer per selezionare UN file, ma per più di uno mi par di capire che niente.

    Opzioni della riga di comando di Esplora risorse

    Forse in rete si trova dell'altro?
    Insomma che explorer accetti l'opzione di selezionare più file in una cartella è assodato, lo facciamo tutti i giorni, ma da programma possibile non si possa, o che nessuno sappia farlo?

  5. #5
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,463
    Originariamente inviato da inconsapevole
    Perché come dicevo nel primo post ho trovato per l'appunto i parametri da passare ad explorer per selezionare UN file, ma per più di uno mi par di capire che niente.
    Chiedo scusa, avevo del tutto tralasciato la prima parte del tuo post in cui già si diceva quanto ho scritto io. Ahem...
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  6. #6
    Utente di HTML.it L'avatar di U235
    Registrato dal
    Mar 2006
    Messaggi
    1,536
    ciao,

    questa (tradotta da c# qui ) fa ciò che ti serve :

    codice:
    Imports System.Collections.Generic
    Imports System.IO
    Imports System.Linq
    Imports System.Runtime.CompilerServices
    Imports System.Runtime.InteropServices
    Imports System.Runtime.InteropServices.ComTypes
    
    NotInheritable Class ShowSelectedInExplorer
        Private Sub New()
        End Sub
        <Flags()> _
        Private Enum SHCONT As UShort
            SHCONTF_CHECKING_FOR_CHILDREN = &H10
            SHCONTF_FOLDERS = &H20
            SHCONTF_NONFOLDERS = &H40
            SHCONTF_INCLUDEHIDDEN = &H80
            SHCONTF_INIT_ON_FIRST_NEXT = &H100
            SHCONTF_NETPRINTERSRCH = &H200
            SHCONTF_SHAREABLE = &H400
            SHCONTF_STORAGE = &H800
            SHCONTF_NAVIGATION_ENUM = &H1000
            SHCONTF_FASTITEMS = &H2000
            SHCONTF_FLATLIST = &H4000
            SHCONTF_ENABLE_ASYNC = &H8000
        End Enum
    
        <ComImport(), Guid("000214E6-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), ComConversionLoss()> _
        Private Interface IShellFolder
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub ParseDisplayName(hwnd As IntPtr, <[In](), MarshalAs(UnmanagedType.[Interface])> pbc As IBindCtx, <[In](), MarshalAs(UnmanagedType.LPWStr)> pszDisplayName As String, <Out()> ByRef pchEaten As UInteger, <Out()> ByRef ppidl As IntPtr, <[In](), Out()> ByRef pdwAttributes As UInteger)
            <PreserveSig()> _
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Function EnumObjects(<[In]()> hwnd As IntPtr, <[In]()> grfFlags As SHCONT, <MarshalAs(UnmanagedType.[Interface])> ByRef ppenumIDList As IEnumIDList) As Integer
    
            <PreserveSig()> _
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Function BindToObject(<[In]()> pidl As IntPtr, <[In](), MarshalAs(UnmanagedType.[Interface])> pbc As IBindCtx, <[In]()> ByRef riid As Guid, <Out(), MarshalAs(UnmanagedType.[Interface])> ByRef ppv As IShellFolder) As Integer
    
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub BindToStorage(<[In]()> ByRef pidl As IntPtr, <[In](), MarshalAs(UnmanagedType.[Interface])> pbc As IBindCtx, <[In]()> ByRef riid As Guid, ByRef ppv As IntPtr)
    
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub CompareIDs(<[In]()> lParam As IntPtr, <[In]()> ByRef pidl1 As IntPtr, <[In]()> ByRef pidl2 As IntPtr)
    
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub CreateViewObject(<[In]()> hwndOwner As IntPtr, <[In]()> ByRef riid As Guid, ByRef ppv As IntPtr)
    
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub GetAttributesOf(<[In]()> cidl As UInteger, <[In]()> apidl As IntPtr, <[In](), Out()> ByRef rgfInOut As UInteger)
    
    
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub GetUIObjectOf(<[In]()> hwndOwner As IntPtr, <[In]()> cidl As UInteger, <[In]()> apidl As IntPtr, <[In]()> ByRef riid As Guid, <[In](), Out()> ByRef rgfReserved As UInteger, ByRef ppv As IntPtr)
    
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub GetDisplayNameOf(<[In]()> ByRef pidl As IntPtr, <[In]()> uFlags As UInteger, ByRef pName As IntPtr)
    
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Sub SetNameOf(<[In]()> hwnd As IntPtr, <[In]()> ByRef pidl As IntPtr, <[In](), MarshalAs(UnmanagedType.LPWStr)> pszName As String, <[In]()> uFlags As UInteger, <Out()> ppidlOut As IntPtr)
        End Interface
    
        <ComImport(), Guid("000214F2-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
        Private Interface IEnumIDList
            <PreserveSig()> _
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Function [Next](celt As UInteger, rgelt As IntPtr, ByRef pceltFetched As UInteger) As Integer
    
            <PreserveSig()> _
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Function Skip(<[In]()> celt As UInteger) As Integer
    
            <PreserveSig()> _
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Function Reset() As Integer
    
            <PreserveSig()> _
            <MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime)> _
            Function Clone(<MarshalAs(UnmanagedType.[Interface])> ByRef ppenum As IEnumIDList) As Integer
        End Interface
    
        Private Class NativeMethods
            <DllImport("ole32.dll", EntryPoint:="CreateBindCtx")> _
            Public Shared Function CreateBindCtx_(reserved As Integer, ByRef ppbc As IBindCtx) As Integer
            End Function
    
            Public Shared Function CreateBindCtx() As IBindCtx
                Dim result As IBindCtx
                Marshal.ThrowExceptionForHR(CreateBindCtx_(0, result))
                Return result
            End Function
    
            <DllImport("shell32.dll", EntryPoint:="SHGetDesktopFolder", CharSet:=CharSet.Unicode, SetLastError:=True)> _
            Private Shared Function SHGetDesktopFolder_(<MarshalAs(UnmanagedType.[Interface])> ByRef ppshf As IShellFolder) As Integer
            End Function
    
            Public Shared Function SHGetDesktopFolder() As IShellFolder
                Dim result As IShellFolder
                Marshal.ThrowExceptionForHR(SHGetDesktopFolder_(result))
                Return result
            End Function
    
            <DllImport("shell32.dll", EntryPoint:="SHOpenFolderAndSelectItems")> _
            Private Shared Function SHOpenFolderAndSelectItems_(<[In]()> pidlFolder As IntPtr, cidl As UInteger, <[In](), [Optional](), MarshalAs(UnmanagedType.LPArray)> apidl As IntPtr(), dwFlags As Integer) As Integer
            End Function
    
            Public Shared Sub SHOpenFolderAndSelectItems(pidlFolder As IntPtr, apidl As IntPtr(), dwFlags As Integer)
                Dim cidl = If((apidl IsNot Nothing), CUInt(apidl.Length), 0UI)
                Dim result = NativeMethods.SHOpenFolderAndSelectItems_(pidlFolder, cidl, apidl, dwFlags)
                Marshal.ThrowExceptionForHR(result)
            End Sub
    
            <DllImport("shell32.dll", CharSet:=CharSet.Unicode)> _
            Public Shared Function ILCreateFromPath(<[In](), MarshalAs(UnmanagedType.LPWStr)> pszPath As String) As IntPtr
            End Function
    
            <DllImport("shell32.dll")> _
            Public Shared Sub ILFree(<[In]()> pidl As IntPtr)
            End Sub
        End Class
    
        Private Shared Function GetShellFolderChildrenRelativePIDL(parentFolder As IShellFolder, displayName As String) As IntPtr
            Dim bindCtx = NativeMethods.CreateBindCtx()
    
            Dim pchEaten As UInteger
            Dim pdwAttributes As UInteger = 0
            Dim ppidl As IntPtr
            parentFolder.ParseDisplayName(IntPtr.Zero, Nothing, displayName, pchEaten, ppidl, pdwAttributes)
    
            Return ppidl
        End Function
    
        Private Shared Function PathToAbsolutePIDL(path As String) As IntPtr
            Dim desktopFolder = NativeMethods.SHGetDesktopFolder()
            Return GetShellFolderChildrenRelativePIDL(desktopFolder, path)
        End Function
    
        Shared IID_IShellFolder As Guid = GetType(IShellFolder).GUID
        Shared pointerSize As Integer = Marshal.SizeOf(GetType(IntPtr))
    
        Private Shared Function PIDLToShellFolder(parent As IShellFolder, pidl As IntPtr) As IShellFolder
            Dim folder As IShellFolder
            Dim result = parent.BindToObject(pidl, Nothing, IID_IShellFolder, folder)
            Marshal.ThrowExceptionForHR(CInt(result))
            Return folder
        End Function
    
        Private Shared Function PIDLToShellFolder(pidl As IntPtr) As IShellFolder
            Return PIDLToShellFolder(NativeMethods.SHGetDesktopFolder(), pidl)
        End Function
    
        Private Shared Sub SHOpenFolderAndSelectItems(pidlFolder As IntPtr, apidl As IntPtr(), edit As Boolean)
            NativeMethods.SHOpenFolderAndSelectItems(pidlFolder, apidl, If(edit, 1, 0))
        End Sub
    
        Public Shared Sub FileOrFolder(path As String, Optional edit As Boolean = False)
            If path Is Nothing Then
                Throw New ArgumentNullException("path")
            End If
    
            Dim pidl = PathToAbsolutePIDL(path)
            Try
                SHOpenFolderAndSelectItems(pidl, Nothing, edit)
            Finally
                NativeMethods.ILFree(pidl)
            End Try
        End Sub
    
        Private Shared Function PathToFileSystemInfo(paths As IEnumerable(Of String)) As IEnumerable(Of FileSystemInfo)
            Dim rect As List(Of FileSystemInfo) = New List(Of FileSystemInfo)
            For Each path__1 In paths
                Dim fixedPath As String = path__1
                If fixedPath.EndsWith(Path.DirectorySeparatorChar.ToString()) OrElse fixedPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()) Then
                    fixedPath = fixedPath.Remove(fixedPath.Length - 1)
                End If
    
                If Directory.Exists(fixedPath) Then
                    rect.Add(New DirectoryInfo(fixedPath))
                ElseIf File.Exists(fixedPath) Then
                    rect.Add(New FileInfo(fixedPath))
                Else
                    Throw New FileNotFoundException("The specified file or folder doesn't exists : " & fixedPath, fixedPath)
                End If
            Next
            Return rect
        End Function
    
        Public Shared Sub FilesOrFolders(parentDirectory As String, filenames As ICollection(Of String))
            If filenames Is Nothing Then
                Throw New ArgumentNullException("filenames")
            End If
            If filenames.Count = 0 Then
                Return
            End If
    
            Dim parentPidl = PathToAbsolutePIDL(parentDirectory)
            Try
                Dim parent = PIDLToShellFolder(parentPidl)
    
                Dim filesPidl As New List(Of IntPtr)(filenames.Count)
                For Each filename In filenames
                    filesPidl.Add(GetShellFolderChildrenRelativePIDL(parent, filename))
                Next
    
                Try
                    SHOpenFolderAndSelectItems(parentPidl, filesPidl.ToArray(), False)
                Finally
                    For Each pidl In filesPidl
                        NativeMethods.ILFree(pidl)
                    Next
                End Try
            Finally
                NativeMethods.ILFree(parentPidl)
            End Try
        End Sub
    
        Public Shared Sub FilesOrFolders(ParamArray paths As String())
            FilesOrFolders(DirectCast(paths, IEnumerable(Of String)))
        End Sub
    
        Public Shared Sub FilesOrFolders(paths As IEnumerable(Of String))
            FilesOrFolders(PathToFileSystemInfo(paths))
        End Sub
    
        Public Shared Sub FilesOrFolders(paths As IEnumerable(Of FileSystemInfo))
            If paths Is Nothing Then
                Throw New ArgumentNullException("paths")
            End If
            If paths.Count() = 0 Then
                Return
            End If
    
            Dim explorerWindows = paths.GroupBy(Function(p) Path.GetDirectoryName(p.FullName))
    
            For Each explorerWindowPaths In explorerWindows
                Dim parentDirectory = Path.GetDirectoryName(explorerWindowPaths.First().FullName)
                FilesOrFolders(parentDirectory, explorerWindowPaths.[Select](Function(fsi) fsi.Name).ToList())
            Next
        End Sub
    End Class
    e si usa così :

    codice:
    ShowSelectedInExplorer.FilesOrFolders("C:\TuoFIle.exe", "C:\TuoAltroFIle.exe")


  7. #7
    Utente di HTML.it
    Registrato dal
    Aug 2011
    Messaggi
    37
    Grazie mille!!

    Sono riuscito con questa classe a far funzionare tutto quel che mi serviva

    Risolto grazie!

  8. #8
    Utente di HTML.it L'avatar di U235
    Registrato dal
    Mar 2006
    Messaggi
    1,536
    Figurati

    Ciao.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.