using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
namespace wsWin
{
#region [ FileAssociation ]
public class FileAssociation
{
public static string Startpath()
{
return System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
}
public static void DoAssociate(string extension, string progID, string description, string icon)
{
if (FileAssociation.IsAssociated(extension))
{
RegistryKey key = Registry.ClassesRoot;
key.DeleteSubKeyTree(extension);
key.Close();
}
FileAssociation.Associate(extension, progID, description, icon, Application.ExecutablePath);
FileAssociation.refreshSystem();
}
public static void DoAssociate(string extension, string progID, string description, string icon, string application)
{
if (FileAssociation.IsAssociated(extension))
{
RegistryKey key = Registry.ClassesRoot;
key.DeleteSubKeyTree(extension);
key.Close();
}
FileAssociation.Associate(extension, progID, description, icon, application);
FileAssociation.refreshSystem();
}
public static void UnAssociate(string extension)
{
if (FileAssociation.IsAssociated(extension))
{
RegistryKey key = Registry.ClassesRoot;
key.DeleteSubKeyTree(extension);
key.Close();
FileAssociation.refreshSystem();
}
}
// Associate file extension with progID, description, icon and application
public static void Associate(string extension, string progID, string description, string icon, string application)
{
Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
if (progID != null && progID.Length > 0)
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
{
if (description != null)
key.SetValue("", description);
if (icon != null)
key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
if (application != null)
key.CreateSubKey(@"Shell\Open\Command").SetValue("",
ToShortPathName(application) + " \"%1\"");
}
}
// Return true if extension already associated in registry
public static bool IsAssociated(string extension)
{
return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
}
public static void refreshSystem()
{
SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
}
[DllImport("Kernel32.dll")]
private static extern uint GetShortPathName(string lpszLongPath,
[Out] StringBuilder lpszShortPath, uint cchBuffer);
[DllImport("shell32.dll")]
static extern void SHChangeNotify(HChangeNotifyEventID wEventId, HChangeNotifyFlags uFlags, IntPtr dwItem1, IntPtr dwItem2);
// Return short path format of a file name
private static string ToShortPathName(string longName)
{
StringBuilder s = new StringBuilder(1000);
uint iSize = (uint)s.Capacity;
uint iRet = GetShortPathName(longName, s, iSize);
return s.ToString();
}
}
#region public enum HChangeNotifyFlags
[Flags]
public enum HChangeNotifyFlags
{
SHCNF_DWORD = 0x0003,
SHCNF_IDLIST = 0x0000,
SHCNF_PATHA = 0x0001,
SHCNF_PATHW = 0x0005,
SHCNF_PRINTERA = 0x0002,
SHCNF_PRINTERW = 0x0006,
SHCNF_FLUSH = 0x1000,
SHCNF_FLUSHNOWAIT = 0x2000
}
#endregion // enum HChangeNotifyFlags
#region enum HChangeNotifyEventID
[Flags]
enum HChangeNotifyEventID
{
SHCNE_ALLEVENTS = 0x7FFFFFFF,
SHCNE_ASSOCCHANGED = 0x08000000,
SHCNE_ATTRIBUTES = 0x00000800,
SHCNE_CREATE = 0x00000002,
SHCNE_DELETE = 0x00000004,
SHCNE_DRIVEADD = 0x00000100,
SHCNE_DRIVEADDGUI = 0x00010000,
SHCNE_DRIVEREMOVED = 0x00000080,
SHCNE_EXTENDED_EVENT = 0x04000000,
SHCNE_FREESPACE = 0x00040000,
SHCNE_MEDIAINSERTED = 0x00000020,
SHCNE_MEDIAREMOVED = 0x00000040,
SHCNE_MKDIR = 0x00000008,
SHCNE_NETSHARE = 0x00000200,
SHCNE_NETUNSHARE = 0x00000400,
SHCNE_RENAMEFOLDER = 0x00020000,
SHCNE_RENAMEITEM = 0x00000001,
SHCNE_RMDIR = 0x00000010,
SHCNE_SERVERDISCONNECT = 0x00004000,
SHCNE_UPDATEDIR = 0x00001000,
SHCNE_UPDATEIMAGE = 0x00008000,
}
#endregion // enum HChangeNotifyEventID
#endregion
}
FileAssociation
最新推荐文章于 2021-03-18 00:51:39 发布