Click here to Skip to main content
12,244,103 members (51,710 online)
Click here to Skip to main content

Stats

16.8K views
1.1K downloads
33 bookmarked
Posted

.NET Shell Extensions - Shell Drop Handlers

, 19 Jan 2013 CPOL
Rapidly create Shell Drop Handler Extensions using .NET
SharpShell.dll
SharpShell Source Code
Dependencies
Apex
Apex.WinForms.dll
SharpShell
packages
Apex.1.5.0
Apex.1.5.0.nupkg
lib
net35
Apex.dll
net40
Apex.dll
sl30
Apex.Silverlight.dll
sl3-wp
Apex.WP7.dll
sl40
Apex.Silverlight4.dll
sl4-wp71
Apex.WP7.dll
sl50
Apex.Silverlight.dll
NUnit.2.6.2
lib
nunit.framework.dll
NUnit.2.6.2.nupkg
Samples
ContextMenu
CountLinesExtension
CountLinesExtension.csproj.vspscc
Key.snk
Properties
Resources
CountLines.bmp
DropHandler
XsdDropHandler
ExampleFiles
Key.snk
Properties
XsdDropHandler.csproj.vspscc
IconHandler
DllIconHandler
DllIconHandler.csproj.vspscc
Key.snk
Properties
Resources
ManagedDll.ico
NativeDll.ico
IconOverlayHandler
ReadOnlyFileIconOverlayHandler
Key.snk
Properties
ReadOnlyFileIconOverlayHandler.csproj.vspscc
Resources
ReadOnly.ico
InfoTipHandler
FolderInfoTipHandler
FolderInfoTipHandler.csproj.vspscc
Key.snk
Properties
PropertySheet
DrivePropertySheet
DrivePropertySheet.csproj.vspscc
Key.snk
Properties
TextFilePropertySheet
Key.snk
Properties
TextFilePropertySheet.csproj.vspscc
SharpShell
Attributes
Diagnostics
Extensions
Interop
Key.snk
NativeBridge
SharpShellNativeBridge32.dll
SharpShellNativeBridge64.dll
Properties
ServerRegistration
SharpContextMenu
SharpDropHandler
SharpIconHandler
SharpIconOverlayHandler
SharpInfoTipHandler
SharpPropertySheet
SharpShell.csproj.vspscc
SharpShellNativeBridge
SharpShellNativeBridge.vcxproj.filters
SharpShellNativeBridge.vcxproj.user
SharpShellNativeBridge.vcxproj.vspscc
Tools
ServerInspector
Properties
ServerInspector.csproj.vspscc
Servers
ServerManager
About
LogView
Properties
Resources
ChangeYourView.png
ContextMenu.png
Icon.png
openHS.png
PlayHS.png
PropertySheet.png
View_Details.png
View_LargeIcons.png
View_List.png
View_SmallIcons.png
View_Tiles.png
ServerDetails
ServerManager.csproj.vspscc
SharpShell.ico
TestShell
ShellExtensionManager
Properties
ShellExtensionManager.csproj.vspscc
SharpShell Tools
Apex.dll
Apex.WinForms.dll
ServerInspector.exe
ServerManager.exe
SharpShell.dll
System.Windows.Interactivity.dll
using System;
using Microsoft.Win32;

namespace SharpShell.Diagnostics
{
    /// <summary>
    /// The ExplorerConfigurationManager can be used to manage explorer configuration relating to the shell.
    /// </summary>
    public class ExplorerConfigurationManager
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ExplorerConfigurationManager"/> class.
        /// </summary>
        public ExplorerConfigurationManager()
        {
            //  Read the configuration.
            ReadConfiguration();    
        }

        /// <summary>
        /// Checks the always unload DLL value.
        /// </summary>
        /// <returns>True if always unload dll is set.</returns>
        private static bool CheckAlwaysUnloadDll()
        {
            using (var alwaysUnloadDLLKey = Registry.LocalMachine.OpenSubKey(KeyName_AlwaysUnloadDll))
            {
                return alwaysUnloadDLLKey != null;
            }   
        }

        /// <summary>
        /// Checks the desktop process value.
        /// </summary>
        /// <returns>True if check desktop process is set.</returns>
        private static bool CheckDesktopProcess()
        {
            using (var explorerKey = OpenExporerSubkey(Registry.CurrentUser, RegistryKeyPermissionCheck.ReadSubTree))
            {
                //  Do we have the value?
                var value = explorerKey.GetValue(ValueName_DesktopProcess, null);

                //  If there's no value, or the value isn't a DWORD, it's not set.
                if (value == null || value is int == false)
                    return false;

                //  We can now check the value explicitly.
                return  ((int)value) == 1;
            }
        }

        /// <summary>
        /// Reads the configuration.
        /// </summary>
        private void ReadConfiguration()
        {
            alwaysUnloadDll = CheckAlwaysUnloadDll();
            desktopProcess = CheckDesktopProcess();

        }

        /// <summary>
        /// Sets the always unload DLL value.
        /// </summary>
        private void SetAlwaysUnloadDll()
        {
            //  Is the key there?
            bool valueSet = CheckAlwaysUnloadDll();

            //  If we're already set correctly, we're done.
            if (valueSet == alwaysUnloadDll)
                return;

            if (alwaysUnloadDll)
            {
                //  Open the explorer key and create the always unload key.
                using (var explorerKey = OpenExporerSubkey(Registry.LocalMachine, RegistryKeyPermissionCheck.ReadWriteSubTree))
                    explorerKey.CreateSubKey(SubKeyName_AlwaysUnloadDLL);
            }
            else
            {
                //  Open the explorer key and delete the always unload key.
                using (var explorerKey = OpenExporerSubkey(Registry.LocalMachine, RegistryKeyPermissionCheck.ReadWriteSubTree))
                    explorerKey.DeleteSubKeyTree(SubKeyName_AlwaysUnloadDLL);
            }
        }

        /// <summary>
        /// Sets the desktop process value.
        /// </summary>
        private void SetDesktopProcess()
        {
            //  Is the key there?
            bool valueSet = CheckDesktopProcess();

            //  If we're already set correctly, we're done.
            if (valueSet == desktopProcess)
                return;

            if (desktopProcess)
            {
                //  Open the explorer key and create the value.
                using (var explorerKey = OpenExporerSubkey(Registry.CurrentUser, RegistryKeyPermissionCheck.ReadWriteSubTree))
                    explorerKey.SetValue(ValueName_DesktopProcess, 1, RegistryValueKind.DWord);
            }
            else
            {
                //  Delete the key value.
                using (var explorerKey = OpenExporerSubkey(Registry.CurrentUser, RegistryKeyPermissionCheck.ReadWriteSubTree))
                    explorerKey.DeleteValue(ValueName_DesktopProcess);
            }
        }

        /// <summary>
        /// Opens the exporer subkey.
        /// </summary>
        /// <param name="hiveKey">The hive key.</param>
        /// <param name="permissionCheck">The permission check.</param>
        /// <returns>
        /// The explorer subkey.
        /// </returns>
        private static RegistryKey OpenExporerSubkey(RegistryKey hiveKey, RegistryKeyPermissionCheck permissionCheck)
        {
            //  Open the explorer key with the desired permissions.
            var explorerKey = hiveKey.OpenSubKey(KeyName_Explorer, permissionCheck);
            
            //  If we don't have it, we've got a critical error.
            if (explorerKey == null)
                throw new InvalidOperationException("Unable to open the Explorer key.");

            //  Return the key.
            return explorerKey;
        }

        /// <summary>
        /// The AlwaysUnloadDLL key name.
        /// </summary>
        private const string KeyName_AlwaysUnloadDll = @"Software\Microsoft\Windows\CurrentVersion\Explorer\AlwaysUnloadDLL";

        /// <summary>
        /// The AlwaysUnloadDLL sub key name.
        /// </summary>
        private const string SubKeyName_AlwaysUnloadDLL = @"AlwaysUnloadDLL";

        /// <summary>
        /// The windows explorer key namme.
        /// </summary>
        private const string KeyName_Explorer = @"Software\Microsoft\Windows\CurrentVersion\Explorer";

        /// <summary>
        /// The dekstop process value name.
        /// </summary>
        private const string ValueName_DesktopProcess = @"DesktopProcess";

        /// <summary>
        /// The always unload dll flag.
        /// </summary>
        private bool alwaysUnloadDll;

        /// <summary>
        /// The desktop process flag.
        /// </summary>
        private bool desktopProcess;

        /// <summary>
        /// Gets or sets a value indicating whether always unload DLL is set.
        /// </summary>
        /// <value>
        ///   <c>true</c> if always unload DLL is set; otherwise, <c>false</c>.
        /// </value>
        public bool AlwaysUnloadDll
        {

            get { return alwaysUnloadDll; }
            set
            {
                alwaysUnloadDll = value;
                SetAlwaysUnloadDll();
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether desktop process is set.
        /// </summary>
        /// <value>
        ///   <c>true</c> if desktop process is set; otherwise, <c>false</c>.
        /// </value>
        public bool DesktopProcess
        {
            get { return desktopProcess; }
            set 
            { 
                desktopProcess = value;
                SetDesktopProcess();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dave Kerr
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 19 Jan 2013
Article Copyright 2013 by Dave Kerr
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid