How to overload the Windows Explorer functionalities? - c#

I want to catch the event of deletion, renaming or copying a file in Windows Explorer. For example, if a folder contains the files "test.a" and "test.b", and that I remove "test.a", then my program will remove the two (same for copy and rename).
I know how to add an entry to the context menu of Windows. But the goal isn't to add functionality to Windows. The goal is to redefine an existing function.

You could use hook for that. See this.

Use the shell notification APIs for this.

Related

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.

Shell Integrate in Windows for a Specific File Type With C#

So I searched for a guide of how to shell integrate your application (add it to the right click menu) with C#, but I couldn't find how to do that only for a specific file type. I know it is possible because WinRar does that. So how can I do that?
There are usually two-ish ways you can implement this.
Registry Keys - You can write keys and values under HKEY_CLASSES_ROOT. If you look at that hive you'll see the extensions on your pc. Look at this article for the details about the keys and values. Something simple like an option to open .myfile types with your application is possible here. Here is a File Association Example
Shell Extensions (Written in COM) : Here you can do more complicated stuff like Handlers. They will get called by Windows so you can do things like paint on menus, or add custom actions when a file is right-clicked. There is more to it here than files, you can even add property sheets and custom tooltips.
You will find some talk about not using .NET to write a Shell Handler**. This applies only to older versions of .NET. Its all ok with .NET4.
This article should help you with a Context Menu Handler in .NET4
** Why was it not recommend:
When you write a shell handler, it gets called by the host process (typically windows explorer), but also things like FileOpenDialogs and FolderBrowser dialogs. So a problem would occur if you wrote a shell extension in .NET 2.0, and a .NET 1.1 app called a File Open Dialog and then your .NET 2.0 shell handler would be called into and your .NET 1.1 app which has an older CLR and there would be version conflict.
So I'm pleased to have found out finally this has been fixed somehow in .NET 4 =)
Windows Explorer right click menus are controlled by the registry. Specifically, the HKEY_CLASSES_ROOT hive.
A good way to get a good idea how everything works in there is to check out HKCR\.txt which shows what will happen for text files in the right click menu. Look at the (Default) key, which points to "txtfile". HKCR\txtfile will then have a subkey HKCR\txtfile\shell\open\command. The (Default) key for that shows the command to open notepad.exe with a parameter of "%1", which indicates the file being opened. Replace the open key with some other name (see print and printto keys in the txtfile key) to add a different custom command to the right-click menu.
Once you get a grasp on what you need to add to integrate your application, you can check out the Microsoft.Win32 namespace for classes to help manipulate the registry via c# code.

Pass multiple files / folders from windows explorer to external application

Hi does anyone know how to get windows explorer to pass multiple files / folders through to an external app (c#) referenced in the registry?
I am current able to act upon a single file / folder using the %1 syntax but not sure how to get explorer to pass through multiple items.
Does anyone know how to do this?
When you select multiple files in Explorer, your shell context menu extension's IShellExtInit::Initialize method will be called and pdtobj contains the selection.
Note writing managed shell extension is not supported.
I don't think this is possible.
When you open multiple files using Explorer, it will launch a separate copy of your program for file. I don't think it's possible to override this behavior.
EDIT: I forgot about shell extensions. This is possible.
To work around this, you could make the subsequent copies communicate with the first one, then exit. Detailed instructions for this are beyond the scope of this answer.
In order to do this reliably you would need to write a shell extension, most likely a sendto implementation.
I haven't written one since vb6 but you can find what looks to be a good managed example here
Or you could use a freeware utility

How can I make an application open when a user double clicks on its associated file?

I am creating an application that uses a certain file format as its data source. I want this application to open whenever the user double clicks on this file, like how MS Word will open when a user double clicks on a Word document. How do I accomplish this? Also how would I populate the data fields using the file that the user selected. Would I use args[] from the program.cs class? I am using c# to code this application.
N.B. I want this association to be made when the application is installed on the host machine without the user doing anything.
FIRST, you need to set up file association, so that your file type is associated with your application and opening the file type will run your application.
You can do the file association programatically, there is some detail here as mentioned:
http://www.codeproject.com/KB/dotnet/System_File_Association.aspx
You can also do it via your Setup project for you application if you have one. This is an easier path for "newbies". Details for using visual studio to get the setup project to add the file association and also set the icon for the file are here:
http://www.dreamincode.net/forums/topic/58005-file-associations-in-visual-studio/
Otherwise if you use InnoSetup, Wix etc then I suppose you could just see instructions for those installers to create the association for you.
SECOND, you need to have your application accept command line arguments. The opened file(s) is(are) passed as a command line argument(s). You need to process the arguments to get the file path/name(s) and open the given file(s). There is a nice description of this here with code:
C# Command Line arguments problem in Release build
In your case, rather than MessageBox.Show(s) in the form shown handler, you would call your bespoke argument parsing method.
For a simple application which only accepts files names to open as arguments, this could be as simple as
foreach (string filePathName in Args)
DoNamedFileOpen(filePathName);
Your code can also have a method that might extract from the file the values for the datafields you are interested in etc.
This is a nice simple approach to the issue of have file associations set on installation of your application, with icons, and having your application handle the opening of those files.
Of course, there are plenty of other options, like run-time file association (asking the user if they want the association), detecting "broken" associations, etc.
This question is a long time here but I hope this is useful for new searches
See this. Or this if you want API information.
ClickOnce supports file associations as of .NET 3.5 SP1, too. In the project's properties, switch to the Publish tab and click the Options button. There's a File Associations section in that dialog that allows you to specify file extensions, descriptions and custom icons.
First, you have to associate the filetype extention with your executeable. On Windows you do this via the registry (search "filetype association windows"). In this question you find some interesting hints: Filetype association with application (C#) Script to associate an extension to a program
Your program has to react on the command line arguments or parameters. In Java, it is indeed the string array of the main method. I would gess, it's the same in C#.
If you don't need to do it pro programatically, right click on the icon, click open with ..., then select 'always use this program ...'.
This is something usually handled by your setup program .. I've used INNO setup for example, and it's trivially simple to arrange for it to adjust user's registry to launch your app when associated file extension is double clicked/opened. It'll even take care of MIME types for you as well as clearing these things on uninstall, which is a very nice thing to do
I managed to solve this issue. I used WIX to create an install file and asked it to associate the file with the application when it installs.

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