Solved

Explorer Context Menu for One Folder Only

Posted on 2014-02-27
8
731 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now