Windows Explorer Context Menu Question - c#

I have a TreeView that represents some files/folders in the Windows file system. i want to be able to right click one and display the standard Windows context menu. The node's tag contains the file path.
I have spent a fair amount of time researching and I found this: http://www.codeproject.com/KB/cs/shellContextMenu.aspx
However, it seems to be missing the "Rename" item. So I want to get that back..
I also want to know how to handle the functions. For example, if the user renames or deletes a file, I want to remove that node/rename that node accordingly.
I am programming in C#, .NET 4.

You need to include the CMF_CANRENAME flag in the uFlags parameter to IContextMenu.QueryContextMenu to indicate that your host application supports renaming items.

Related

How to add my application to the context menu for .docx file types [duplicate]

I've developed an application that load an image using the context menu of window (right click on the file) and for the moment is working, but the reg key is on
HKEY_CLASSES_ROOT\*
and it works with all files.
I want that the menu item on the context menu should be displayed only with .jpg files.
How can I do that? Which registry keys should I use?
Identify the file type (ProgID) for .jpg files
This can be done by checking the default value of HKEY_CLASSES_ROOT\.jpg. It could be anything based on what you've installed, but for the purposes of this example, we'll call it jpegfile, a common default.
Set the context menu item (verb) properties for that file type
You can set per-user context menu items in HKEY_CURRENT_USER\Software\Classes\jpegfile\shell. This key has a list of verbs for the file type. There is a similar key in HKEY_LOCAL_MACHINE\Software\Classes\jpegfile\shell, and these are the system defaults for the file type. You can put a verb key there too, but if the same key exists in HKCU, it will be overridden, so be advised.
Set the command value
The bare minimum key value that needs to be set to get it to work is the default value of the command subkey. You need to set that with the path to your application, like so: HKEY_CURRENT_USER\Software\Classes\jpegfile\shell\open_with_myapp\command would be set to "c:\path\to\myapp.exe" "%1". Now a context menu for .jpg files will have a "open_with_myapp" item which will launch your app when clicked, and pass the file name of the selected file as a parameter. Of course, how your application processes parameters is up to you, so you'd need to set the parameter string to something your app can process.
Set other verb properties
I'd imagine you're probably going to want the context menu item to read something a little more friendly than the key name. You can have the context menu display whatever label you want for your item by setting the default value of that key (open_with_myapp).
That's your basic overview. Definitely check out my answer to this question about associating a file, which has a similar answer:
Create registry entry to associate file extension with application in C++
There's another key on the registry that works independently of user's default programs: HKEY_CLASSES_ROOT\SystemFileAssociations. Since nobody mentioned it on this question... No need to check ProgID before adding the context menu item. Example:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mkv\shell\subtitle]
#="Search subtitles..."
[HKEY_CLASSES_ROOT\SystemFileAssociations\.mkv\shell\subtitle\command]
#="\"D:\\Tools\\subsearch.exe\" \"%1\""
Reference: https://learn.microsoft.com/en-us/windows/desktop/shell/app-registration#registering-verbs-and-other-file-association-information
Additional Considerations:
The HKEY_CLASSES_ROOT subtree can be written to but in general is a view formed by merging
HKEY_CURRENT_USER\Software\Classes
file type registration visible to the current user only
HKEY_LOCAL_MACHINE\Software\Classes
globally register a file type on a particular computer
You can register to those classes instead/aswell
The (ProgID) defined verbs have priority over the same ones defined in ...\SystemFileAssociations\ , but are dependent on that particular Application, When that application uninstalls, it would normally delete its registry entry, along with what modifications/additions you may have done under that key. Or if the default (ProgID) is changed, your modifications will no longer be in effect.
The ...\SystemFileAssociations\ registrations are stable even when users change/uninstall the default programs.
Will publish my working solution derived from the previous answer (and one of its author's other answer).
It also adds an icon. I used it for all file types and didn't have administrator's privileges. The subitem * didn't exist in my registry, I created it myself.
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\*]
[HKEY_CURRENT_USER\Software\Classes\*\shell]
[HKEY_CURRENT_USER\Software\Classes\*\shell\open_with_notepad_pp]
#="Open with Notepad++"
"icon"="C:\\portable\\npp.7.9\\notepad++.exe"
[HKEY_CURRENT_USER\Software\Classes\*\shell\open_with_notepad_pp\command]
#="\"C:\\portable\\npp.7.9\\notepad++.exe\" \"%1\""
UPDATE
Replace * with something like .svg and only for this extension the menu item will be shown.

Edit Windows Context Menu in the C#

I would like a particular type of file (eg. Namefile.ext2) read all the names preceded by a #
Sample contents of the file:
#nameone
#nametwo
#namethree
When I click the right mouse button on the ext2 file extension beyond the standard options (like: open, properties, etc ...) I would like to be:
contents of the file > nameone
nametwo
namethree
Then, select the item (eg. nameone) pass this parameter to my program running in the background / or services
Do you need to modify the registry somehow? I will be grateful for tips on how to achieve the desired effect.
What you are asking about is called 'shell extension'. Basically it requires some knowledge of COM objects programming, but .NET made things a bit easier in that matter.
Shortly: you have to develop a piece of code which will reads the file and generates menu items dynamically (which may be tricky but possible). That code needs to be registered in the system as COM object.
Before it starts working you have to associate file extension with COM object you created.
Perhaps this article can explaint it a bit more:
http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus

Windows 8 specific file type context menu association

There is a need to add item to particular file type's context menu. In Windows 7 and previous versions that's done by adding
HKEY_CLASSES_ROOT\<extension>\shell\<commandname>\command
key with value of
#="<path_to_app> \"%1\""
to registry.
In Windows 8 that doesn't work. Even after deleting PersistentHandler key of respective type :
What's interesting, if shell/command keys are written in HKEY_CLASSES_ROOT\*\ section, context menu item appears in menu for all files as expected, but that's a huge overhead in case what I need is add my application to only 2-3 filetypes without breaking any existing associations.
I'm using Microsoft.Win32.Registry.ClassesRoot in C# to implement this logic, but the problem is definitely not in code because a) it works in Windows 7 b) manual editing with administrator previledges in Windows 8 doesn't add needed association.
You must have noticed that there wasn't a "shell" key originally there, even though .avi files certainly have an Open context menu item. There's an extra level of indirection, note the default value of the .avi key. That's the ProgId associated with the .avi filename extension.
Follow that to the WMP11.AssocFile.AVI key to find the shell commands, add your command there.
"HKEY_CLASSES_ROOT\SystemFileAssociations\video" is "real" subtree for .avi files in systems with non-WMP default media player. .mkv file type that i was interested in also creates another subtree - "mkv_auto_file". Don't know why it's done and where it's documented, but my problem was solved. Thanks Hans for guiding.

How to change an icon for one single file of the specific type?

I know how to change an icon for a whole file type using registry and file type association.
I also know how to change an icon for a separate folder using desktop.ini file.
My questions is whether it is possible to change an icon for one specific file, let's say one.doc file?
I cannot find a way doing it, yet some upload programs seem to be able to change an icon of the currently uploaded file, while all other files of this type stay with the standard icon. How do they do it?
I can accept a solution in any language; VB, C++, C# - anything goes, though C# is preferable.
Thanks
You can use an IconHandler to allow icons to be customized on a file-by-file basis. Note that shell extensions should not be written in managed code, so C++ is the language of choice here.
Note also that it's highly unusual to be installing an icon handler for another application's file type.
I realize that this thread is pretty old. But for users who have less experience in coding, a good alternative is the following.
Put your target file somewhere.
Create a shortcut item to that file.
Now you can change the icon of the shortcut using its property window.
Just another solution. More of a workaround really.
If you create a SHORTCUT file to that file, then simply RIGHT click on that SHORTCUT, you get the options to change the ICON.

How do you determine the last process to modify a file?

I know how to get the user that last modified a file, but does Windows track the process that made the modification as well? If so, is there an API for looking that up?
No. It is not recorded.
You could enable Object Access Auditing on a particular folder (I wouldn't recommended using on the general file system). See this post and use with caution!
You might be able to use .NET's FileSystemWatcher class.
Windows does NOT track the process that modifies a file. Best you can hope for is to actively watch the file(s) you are interested in and take note when an application opens them. I've seen applications that do this (eg. Sysinternals FileMon) but do not know off the top of my head how to accomplish it.
If you want to determine the process that is modifying the file during the runtime of your program, you could use the Windows API or a minifilter driver to track what every process on the system is doing (similar to using Sysinternals FileMon with custom filters, as Boo pointed out), but that will apply only during the runtime of your "capture" program. Once the file is modified, however, all the traces are gone.
Could you use Windows security auditing to log access to the file?
You could use procmon.exe from https://live.sysinternals.com/procmon.exe
This is a live monitor, so not much use retrospectively.
Deselect (uncheck) the "File | Capture Events" menu item.
Clear the currently displayed events by selecting the "Edit | Clear
Display" menu item.
Configure a filter by:
a. Select the Process Monitor "Filter | Filter..." menu item. This will
display the "Process Monitor Filter" dialog.
b. Set a new filter by setting the "Display entries matching those
conditions" fields:
c. (pick)Path (pick)contains whatever.txt then (pick)Include
d. Click the "Add" button to add the new filter.
derived from instructions here: http://answers.perforce.com/articles/KB/3035

Categories

Resources