Windows Shell Integration using .NET - c#

What's the easiest/best way to register your program in explorers right-click menu using .NET and C#?
i.e. I would like to be able to right-click on an item in windows explorer and get a "Edit with MyProgram"
This is the closest thing to a tutorial I could find but it mostly just dips into Win32 from .NET and is also outdated. How should this be done now?

If you just want to add menu items, then a shell extension is overkill. You can register a command line in the registry which will run your exe with the selected file(s) as the parameter. Shell extensions are really only required if you want to change explorer's behavior, add custom icons, or hook shell based file operations.
http://www.codeproject.com/KB/shell/SimpleContextMenu.aspx
If a shell extension is what you need, you're best writing a thin wrapper in unmanaged code that calls out to another process that is your .NET application through some sort of cross process communication channel. Due to all the potential versioning issues, it's not recommended to load the .NET runtime into the explorer process.

Related

c# How to programmatically disable Windows 8 specific features

This question is asked alot, but I couldnt find working method / way to do it - except for a third party application.
I am pretty sure, or atleast I am being very hopeful that solution for this problem does exist.
As the title says, I want to disable window 8 gestures just like every third app is doing (SkipMetroSuite, ClassicShellMenu or w/e).
I need it to be built in in my app because I cant install anything on the compter my app is dedicated to but my app itself...
Is there a way to do it in C#?
EDIT:
I personally asked the developer of Classic Shell Menu how his programs works, here is the answer:
The principle is to inject a message hook in the thread of window with
class “ApplicationManager_DesktopShellWindow”, then listen for mouse
messages sent to windows with class “EdgeUiInputWndClass”, and hide
those windows. When my program exists it reshows all windows that it
has hidden.
He also mentioned I can find the solution here:
Classic Shell src
But there's one problem, the solution is in c++ and I have no Idea how to port it to c# so I would appreciate your help.
The solution is in ClassicStartMenuDLL.cpp which is in ClassicStartMenuDLL Solution.
The first step to what you want to do is to disable Metro mode (the start screen tiles).
You can achieve this via a registry edit, which you can do programmatically.
The entry of interest is the following:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RPEnabled
You need to set this to 0
Next, you want to disable the 'hot corners'. This is also a registry edit which can be done programmatically.
The entry of interest is the following:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ImmersiveShell
Create a new key called EdgeUI, and under thay key create the following DWORD entries:
DisableTLcorner
DisableTRcorner (Windows 8.1+)
DisableCharmsHint
Set both values to 1
Since these are both HKCU settings (i.e. current user), then a simple log-off is all that is required for them to take effect.
Alternatively you can kill the explorer process, though it is not recommended.
If it is not working for you, try to test it with a ready-made registry file first, since you might be doing something wrong -> Disable Charms & Switcher

Custom icon that opens URL

I've only used Visual Studio a handful of times.
Can I make an executable with a custom icon that strictly opens a URL in a web browser?
I am assuming Visual Studio will be the best tool to help me achieve this, although I am open to better options.
It has to have a custom icon and be a stand alone file.
Thanks.
if you don't need it to be an executable you could quickly create a windows shortcut.
http://support.microsoft.com/kb/140443
You can personalize the icon too.
As PrashantGupta has pointed out you can only use a subsets of windows icons if you want it to be a single file.
Sure,
Just write a single line console app with this as your code
System.Diagnostics.Process.Start("http://my.url.com");
You can configure an icon from within visual studio easily too.
Sure, did I get this right: You need an EXE with custom icon and launches a URL?
If you choose C# as your development language the following code will achieve what you want:
namespace URLLauncher
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process.Start("http://www.google.com/");
}
}
}
Changing the icon is also quite straightforward in visual studio as well. See this: Adding an icon to my finished application
Also try not to make your application a "Console" application as that will pop up a "black console window" when you launch the app if you need a double-click interface from the user (which I infer from your wanting an ICON).
Hope this helps.
If you decide to go the C++ route, passing a URL to the ShellExecute function will let you launch a website using the user's preferred browser.
See http://support.microsoft.com/kb/224816
And you don't really need anything from the C or C++ runtime for this, so compile with /NODEFAULTLIB use the /ENTRYPOINT linker option to skip all that, make your executable truly tiny, and have very few dependencies (meaning none that aren't included in every version of Windows since 95). It'll start faster too, not needing to run .NET or any library initialization code.
Add your icon in the usual way using the Resource Compiler.
Solves the problem, not necessarily with the requested tool
While we are at-it, so to speak, I thought someone must have invented this wheel before, so if you are not interested in any development what-so-ever (check licenses before use tho), here are some online tools to do what you want:
Web shortcut producer
Remember to scan the EXE for for malware. Hope it helps :-)
The Visual Studio route has a lot of advantages and will work for this situation. Although, it is overkill for the project I am working on.
I have chosen to go with this solution:
Build a .bat file with the command:
start http://www.google.com
Then use a bat to exe converter which allows icon assignment.
Worked like a charm and quick.

Should I incorporate a setup wizard at first run, or use an MSI?

My program has a handful of settings that need to be established before it can operate correctly. I would like the user to be shown a setup wizard either at first run or at install that has them set everything up to their needs. Right now my program does not use an installer, it just runs from its exe file. The program does offer the option to change these settings when it is running, however I want the user to set the settings first before the program runs. My options seem to be:
Use a setup wizard (either my own or one created such as this link.)
Make the program use an MSI and install. Several have pointed out that an MSI will allow for a program set up while installing which would satisfy the need I have.
As an MSI option sounds like it is the norm for programs I should perhaps learn about those. But I am still curious for other peoples input on this particular problem. For those who are going to recommend the MSI solution, I'm new to this particular aspect of programming, any recommended links are greatly appreciated.
I think it really depends on what your application deployment does. If it simply copies some files and registry entries an MSI will handle most of your needs, including upgrades. Here is a similar discussion which may help you: What is the best SIMPLE replacement for VS Setup-project Installer for WinXP + WPF + .NET 4.0?
If your deployment process involves custom and/or complex tasks, a customized wizard is better because it gives you more control. MSI packages are very limited when it comes to customization.
We do something similar:
Whenever the application is launched, we check the settings storage (usually a database) and if anything needs to be set, we launch the setup wizard.
If the user cancels the setup for some reason, the application is terminated, meaning that the user can't proceed to the main application until the initial configuration is complete.
Having spent an inordinate amount of time configuring (or attempting to configure) installers (Installshield, Installaware, etc), I can promise you that this is the most effective, efficient way to accomplish your goal.
There are some things that you should (and in some cases, have) to do in the installers, but from your description, they don't apply to your situation.

Create two (slightly) different executables from same visual c# project

I have a visual c# project from which I want to compile two executables: Full.exe, and Limited.exe. Limited.exe simply hides a couple of UI controls.
I'm thinking of adding another pair of solution configuration (DebugLimited and ReleaseLimited) which simply sets a flag, and then in my build script just build my app with Release configuration and with ReleaseLimited configuration.
Is there an easier way to accomplish this?
If you really want 2 differnet executables this approach works. Make sure that each flavor builds into its own directory. "Build-> batch build" option will let you build all of them at once.
If goal is to have different UI instead of different executables you can use setting in .config file to control what UI to show/hide (with appropritate code to turn on/off controls).
I would make a couple of projects. One called Full and the other called Limited. They are simply wrappers around your main project and set your appropriate options before starting. This way, on a full build of your solution you always get both executables.

Communicate between a COM DLL and C#

I asked a question the other day regarding overlay icons. With help, I figured out how to get that working.
Here's how an icon overlay works (as far as I understand): Before the shell draws an icon it contacts all the icon overlay handlers in the system to determine whether it should draw an overlay on the that particular icon.
My setup:
I have a registered Shell Extension (Icon Overlay Handler) that I want to use to display icon overlays. Also, I have a .NET application (C#) that will write to a database (SQLite, most likely) with the names, etc. of all the files and folders I want to display an overlay on.
My problem is:
How do I get the Shell Extension (I think its basically a COM DLL) to call back into my .NET application? Or is that overkill and should I just have the Shell Extension read from the database directly?
Possible solutions?
Have the Shell Extension (icon overlay handler) read the database and determine whether to show overlay.
Have the Shell Extension call back into a .NET application to determine whether to show the overlay.
I hope this makes sense, if not, I'll try to elaborate.
A COM DLL cannot talk to .NET assembly directly. You might need to expose your .NET assembly as COM object and talk to this COM object instead. But this might in fact be an overkill in your scenario. Another option would be to expose the functionality that talks to the database in your .NET assembly as some interoperable service (WCF?) that might be called from the shell etension.
Yes, if you mark your assembly as COM visible and run regasm, then your COM dll can import the generated type library and call CoCreateInstance to get a reference to your .NET classes.
HOWEVER, it is a little scary to pull the .NET framework into a shell extension. So you might want to make sure that the .NET code is invoked out-of-process... ie CLSCTX_LOCAL _SERVER to CoCreateInstance.

Categories

Resources