Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Explorer Context Menu for One Folder Only

Posted on 2014-02-27
8
Medium Priority
?
853 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

877 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