Solved

Explorer Context Menu for One Folder Only

Posted on 2014-02-27
8
780 Views
Last Modified: 2014-03-03
Hi,

I want to Add Right Click Context Menu  for only one Folder (e.g. C:\MyFolder\)

If i right click on any file inside C:\MyFolder, My Context Menu need to display. Outside C:\MyFolder no need to display My Context Menu. (like dropbox context menu for only one folder)

I am able to global Context Menu but not able to specific Folder Context Menu.


thanks

Kalpesh
0
Comment
Question by:Kalpesh Chhatrala
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39892590
In your context menu's 'Initialize()' (http://msdn.microsoft.com/en-us/library/windows/desktop/bb775094%28v=vs.85%29.aspx) method - which is inherited from 'IShellExtInit' - check the folder name like

HRESULT CMyContextMenu::Initialize(LPCITEMIDLIST, LPDATAOBJECT pDataObj, HKEY)
{
FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT,
                  -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };
HDROP     hDrop;
 
  // Look for CF_HDROP data in the data object. If there
  // is no such data, return an error back to Explorer.
  if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
    return E_INVALIDARG;
 
  // Get a pointer to the actual data.
  hDrop = (HDROP) GlobalLock ( stg.hGlobal );
 
  // Make sure it worked.
  if ( NULL == hDrop )
    return E_INVALIDARG;

  TCHAR acFile[MAX_PATH];

  DragQueryFile(hDrop,	i,acFile,sizeof( acFile));

  if (!_tcscmp(acFile,_T("c:\\MyFolder"))) 
    m_bShowMenu = TRUE; // evaluate this flag in 'QueryContextMenu()' to check if you should display the menu

  //...

  return S_OK;
}

Open in new window

0
 
LVL 16

Author Comment

by:Kalpesh Chhatrala
ID: 39892611
I need code in C#. by mistake i added C++ as Topic.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39892687
That works in C# as well - see http://www.codeproject.com/Articles/174369/How-to-Write-Windows-Shell-Extension-with-NET-Lang ("How to Write Windows Shell Extension with .NET Languages") - i.e.

        public void Initialize(IntPtr pidlFolder, IntPtr pDataObj, IntPtr hKeyProgID)
        {
            if (pDataObj == IntPtr.Zero)
            {
                throw new ArgumentException();
            }

            FORMATETC fe = new FORMATETC();
            fe.cfFormat = (short)CLIPFORMAT.CF_HDROP;
            fe.ptd = IntPtr.Zero;
            fe.dwAspect = DVASPECT.DVASPECT_CONTENT;
            fe.lindex = -1;
            fe.tymed = TYMED.TYMED_HGLOBAL;
            STGMEDIUM stm = new STGMEDIUM();

            // The pDataObj pointer contains the objects being acted upon. In this 
            // example, we get an HDROP handle for enumerating the selected files 
            // and folders.
            IDataObject dataObject = (IDataObject)Marshal.GetObjectForIUnknown(pDataObj);
            dataObject.GetData(ref fe, out stm);

            try
            {
                // Get an HDROP handle.
                IntPtr hDrop = stm.unionmember;
                if (hDrop == IntPtr.Zero)
                {
                    throw new ArgumentException();
                }

                // Determine how many files are involved in this operation.
                uint nFiles = NativeMethods.DragQueryFile(hDrop, UInt32.MaxValue, null, 0);

                // This code sample displays the custom context menu item when only 
                // one file is selected. 
                if (nFiles == 1)
                {
                    // Get the path of the file.
                    StringBuilder fileName = new StringBuilder(260);
                    if (0 == NativeMethods.DragQueryFile(hDrop, 0, fileName,
                        fileName.Capacity))
                    {
                        Marshal.ThrowExceptionForHR(WinError.E_FAIL);
                    }
                    this.selectedFile = fileName.ToString();

                    if(this.selectedFile == "c:\MyFolder") this.showMenu = true;
                }
                else
                {
                    Marshal.ThrowExceptionForHR(WinError.E_FAIL);
                }

            }
            finally
            {
                NativeMethods.ReleaseStgMedium(ref stm);
            }
        }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Author Comment

by:Kalpesh Chhatrala
ID: 39893198
Hi,

Thanks for solution. its working for me for files.

I need Context Menu for Folder also. can you help me for it ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39893278
That should actually work for foldes the same way...
0
 
LVL 86

Expert Comment

by:jkr
ID: 39893292
Hm, let me re-think that... How did you register the shell extension?
0
 
LVL 16

Author Comment

by:Kalpesh Chhatrala
ID: 39893850
I used code from provided link by you.

http://www.codeproject.com/Articles/174369/How-to-Write-Windows-Shell-Extension-with-NET-Lang

just added one condition for it.
0
 
LVL 16

Author Closing Comment

by:Kalpesh Chhatrala
ID: 39900291
Thanks.

Its working fine.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question