Prince Mishra
Prince Mishra

Reputation: 311

Create windows right click context menu for SPECIFIC folders

How can I create a context menu that appears for files/folders inside a particular folder.
Say there is a directory "D:\RandomCodes"
How do I create a custom context menu item "Open in MyApp" for any file/folder inside this? This menu item should not appear for any other directory. I know if I add the entry in HKCR/Directory/Shell, it'll work, but then it'll appear for all files and folders everywhere. Please guide me through this.

Upvotes: 5

Views: 7615

Answers (3)

Bobby Byrnes
Bobby Byrnes

Reputation: 430

I know this is a pretty old question, but to anyone who comes across this in the future, I found the simplest way was to add a String Value to the key called "AppliesTo" and set its value to "under:{path}"

In my example, I want it to only look in the T Drive, so my String value is "AppliesTo":"under:T:".

In C#, this is easily accomplished with the following:

RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Folder\\Shell", true);
RegistryKey newkey = _key.CreateSubKey("My Menu Item");
newkey.SetValue("AppliesTo", "under:T:");

RegistryKey subNewkey = newkey.CreateSubKey("Command");
subNewkey.SetValue("", "C:\\yourApplication.exe");
subNewkey.Close();

newkey.Close();
_key.Close();

Upvotes: 5

tol
tol

Reputation: 61

example:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\NetBeans]
"AppliesTo"="System.ItemPathDisplay:\"NetBeansProjects\""
@="Open with NetBeans"

[HKEY_CLASSES_ROOT\Folder\shell\NetBeans\command]
@="\"C:\\Program Files\\NetBeans 7.2.1\\bin\\netbeans64.exe\" --open \"%1\""

more info here:

http://msdn.microsoft.com/en-us/library/cc144171%28VS.85%29.aspx

Upvotes: 6

Xearinox
Xearinox

Reputation: 3234

Is possible modifing your code for IShellExtInit:

    STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST pidl,LPDATAOBJECT pDataObj,HKEY hk)
    {
    // Initialize can be called more than once

    // If Initialize has already been called, release the old
    // IDataObject pointer.
    if (m_pDataObj)
    { 
        m_pDataObj->Release(); 
    }

    // If a data object pointer was passed in, save it and
    // extract the file name. 
    if (pDataObj == NULL)
        return E_INVALIDARG;

        m_pDataObj = pDataObj; 
        pDataObj->AddRef(); 

        STGMEDIUM   medium;
        FORMATETC   fe = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
        UINT        uCount;

        HRESULT hr = pDataObj->GetData(&fe, &medium);
        if (FAILED(hr))
            return E_INVALIDARG;

        // save the file name
        if (DragQueryFile((HDROP) medium.hGlobal, 0xFFFFFFFF, NULL, 0)==1) 
        {
            DragQueryFile((HDROP) medium.hGlobal, 0, m_szFile, 
                sizeof(m_szFile));

            if (lstrcmpi(m_szFile, "D:\\RandomCodes") == 0) 
            {
                hr = NOERROR;
            }
            else 
                hr = E_INVALIDARG;
        }
        else
            hr = E_INVALIDARG;

        ReleaseStgMedium(&medium);

        return hr;

}

Upvotes: 4

Related Questions