Hosting Windows Shell Explorer in my WPF App

前端 未结 4 1640
独厮守ぢ
独厮守ぢ 2020-12-29 14:44

Is it possible to embed the Windows Explorer file/folder browser view in a WPF or a WinForms window?

I basically want to host the file/folder browser as part of my a

相关标签:
4条回答
  • 2020-12-29 15:11

    As far as I know, there isn't any File/Folder browser control out of the box in Windows Forms nor WPF.
    However there are commercial third-party controls that offer a Windows Explorer-like interface to browse files and folders on disk. Take a look at these products from LogicNP:

    • FileView Control
    • FolderView Control

    Alternatively, you could host the Windows Forms WebBrowser control in WPF and use it to browse the filesystem, since it is integrated with the Windows shell.
    In XAML you could do something like this:

    <Window x:Class="Samples.FilesystemBrowser"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="Filesystem Browser">
        <StackPanel>
            <WindowsFormsHost>
                <wf:WebBrowser Url="C:\" />
            </WindowsFormsHost>
        </StackPanel>
    </Window>
    

    Note, that with .NET 3.5 SP1 Microsoft added a native WPF WebBrowser control, so you can optionally use that instead:

    <Window x:Class="Samples.FilesystemBrowser"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Filesystem Browser">
        <StackPanel>
            <WebBrowser Source="C:\" />
        </StackPanel>
    </Window>
    
    0 讨论(0)
  • 2020-12-29 15:14

    The approach in my answer isn't one I would necessarily recommend, as it is basically a huge hack. However, it is possible to 'host' pretty much any Windows application inside another. Caveats:

    • This is a big fat hack
    • I'm not sure at all how well it plays with the various security features that are e.g. in Vista
    • You'll be working with low-level and sometimes underdocumented APIs the whole time, and doing things that are not what the original designers anticipated.
    • I don't know the various APIs well enough to tell you exactly what to do, so this is a very rough sketch...

    The basic approach would be to:

    1. Start up a new explorer.exe process
    2. Get its HWnd
    3. Using p/invoke calls into various windows APIs (mostly shell32.dll), re-parent it into a NativeWindow or UserControl of your own.
    4. You can then replace its WndProc message handler with your own, subclassing it to inject your own application-specific behaviors. (C++ example; stackoverflow question WRT calling the old/default WndProc; googling will produce a lot of answers. I've done this before in C# and (ick) VBA) This will let you replace various UI behaviors with your own, albiet at a very low level. (It's dependent on how exactly explorer is implemented: higher-level things like menu clicks can get their own message and so be easier to handle; but other aspects of explorer's behavior you might only get the raw mouse messages.)

    You'll want Spy++ in order to figure out what messages happen when.

    Yes, this is a great way to build lots of very ugly and fragile code, but it's (a) sometimes the only way to get things working right; and (b) great for learning what's going on under the hood of Windows.Forms / MFC / etc.

    0 讨论(0)
  • 2020-12-29 15:21

    Windows Vista’s Shell introduced a new control which implements the IExplorerBrowser interface; this is the recommended method of hosting a Windows Shell filesystem view within your application. Developers building applications using .NET can use the wrapped version of the ExplorerBrowser control available in the Windows API CodePack for .NET.

    Please note that this interface is only available on Windows Vista and later. If your application needs to run on earlier Windows versions, you will need to fallback to the old WebOC implementation on those platforms.

    http://msdn.microsoft.com/en-us/library/bb761909(VS.85).aspx http://code.msdn.microsoft.com/WindowsAPICodePack

    This is recommended over hosting the web browser control due to a Win7 issue described here: http://blogs.msdn.com/ieinternals/archive/2009/12/30/Windows-7-Web-Browser-Control-will-not-browse-file-system.aspx

    0 讨论(0)
  • 2020-12-29 15:26

    I've written an open-source library for embedding windows explorer in .NET applications. You can take a look at http://gong-shell.sourceforge.net/

    It exposes WinForms controls, as well as providing a .NET language interface to the Windows Shell. It's licenced under the GPL, so using it in a similarly licensed application is free, however if you want to use it in a commercial application drop me an email and I'm sure we can work something out!

    0 讨论(0)
提交回复
热议问题