如何用C#来做Shell Extenstion
下面给出个例子,欢迎大家来讨论,它们到底能干什么?
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
//using System.Windows.Forms;
using Microsoft.Win32;
using ShellExt;
using System.Text;
using System.IO;
//[assembly:AssemblyKeyFile("..\\..\\CleanVS.snk")] // Strong name keyfile for this assembly
[assembly:ComVisible(false)] // Make sure that no interfaces are registered with COM
namespace CleanVS
{
[Guid("2AA8DDCB-0540-4cd3-BD31-D91DADD81ED3"), ComVisible(true)]
public class CleanVS : IContextMenu, IShellExtInit
{
public CleanVS() {}
const string clsid = "{2AA8DDCB-0540-4cd3-BD31-D91DADD81ED3}";
IDataObject m_dataObject = null;
uint m_hDrop = 0;
#region Registration
[System.Runtime.InteropServices.ComRegisterFunctionAttribute()]
static void RegisterServer(String zRegKey)
{
try
{
RegistryKey root;
RegistryKey rk;
root = Registry.CurrentUser;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\" +
"CurrentVersion\\Explorer", true);
rk.SetValue("DesktopProcess", 1);
rk.Close();
// For Winnt set me as an approved shellex
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\" +
"CurrentVersion\\Shell Extensions\\" +
"Approved", true);
rk.SetValue(clsid, "CleanVS Shell Extension");
rk.Close();
root = Registry.ClassesRoot;
rk = root.CreateSubKey("Folder\\shellex\\" +
"ContextMenuHandlers\\CleanVS");
rk.SetValue("", clsid);
rk.Close();
}
catch(Exception e)
{
System.Console.Error.WriteLine(e.ToString());
}
}
[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]
static void UnregisterServer(String zRegKey)
{
try
{
RegistryKey root;
RegistryKey rk;
// Remove ShellExtenstions registration
root = Registry.LocalMachine;
rk = root.OpenSubKey("Software\\Microsoft\\Windows\\" +
"CurrentVersion\\Shell Extensions\\" +
"Approved", true);
rk.DeleteValue(clsid);
rk.Close();
// Delete ShellCmd regkey
root = Registry.ClassesRoot;
root.DeleteSubKey("Folder\\shellex\\ContextMenuHandlers\\" +
"CleanVS");
}
catch(Exception e)
{
System.Console.Error.WriteLine(e.ToString());
}
}