一、创建桌面快捷方式
createassociation函数是一个用于在Windows操作系统中创建关联的函数。在Windows操作系统中,当我们双击某个文件时,系统会根据不同文件类型的扩展名来调用不同的应用程序。如果我们想要把某个应用程序的文件关联为某个扩展名的文件,则需要使用到createassociation函数。
此处通过创建桌面快捷方式的实例来说明createassociation函数的使用:
Shell objShell = new Shell();
string filePath = @"C:\Windows\System32\notepad.exe"; //需要创建快捷方式的程序路径
string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notepad.lnk"; //快捷方式保存路径
IWshShortcut shortcut = (IWshShortcut)objShell.CreateShortcut(shortcutPath); //创建快捷方式对象
shortcut.TargetPath = filePath; //设置目标路径
shortcut.WorkingDirectory = Path.GetDirectoryName(filePath); //设置工作目录
shortcut.WindowStyle = 1; //设置窗口样式
shortcut.Description = "Notepad Shortcut"; //设置描述信息
shortcut.Save(); //保存快捷方式
二、文件类型关联
除了创建桌面快捷方式外,createassociation函数还可以用于文件类型关联。例如你想让系统把.mp3文件关联为某个音乐播放器程序,则可以使用createassociation函数来实现。下面是一个简单的文件类型关联的示例:
[DllImport("shell32.dll")]
static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
static extern uint AssocCreateForClasses([In] ref Guid clsid, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppv);
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
static extern int AssocSetPerceivedType([In] ref Guid clsid, [In] uint type);
static void Associate(string ext, string appName)
{
Guid clsid = new Guid("YOUR_APPLICATION_CLSID");
Guid riid = new Guid("YOUR_IASSOCWSTR_CLSID");
object obj;
AssocCreateForClasses(ref clsid, ref riid, out obj);
IAssocWSTR assocStr = (IAssocWSTR)obj;
uint key = 0;
assocStr.GetKeyValue(new IntPtr(0), key, "PerceivedType", out key);
AssocSetPerceivedType(ref clsid, key);
SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}
Associate(".mp3", "Music Player");
三、设置默认打开方式
使用createassociation函数可以轻松地将一个程序设置为某个文件类型的默认打开方式,例如把Windows Media Player设置为MP4文件的默认打开方式。
string extension = ".mp4";
string progId = "WMPlayer";
string friendlyName = "Windows Media Player";
try
{
Registry.ClassesRoot.CreateSubKey(extension).SetValue(null, progId);
Registry.ClassesRoot.CreateSubKey(progId).SetValue(null, friendlyName);
Registry.ClassesRoot.CreateSubKey(progId + @"\DefaultIcon").SetValue(null, @"%ProgramFiles%\Windows Media Player\wmplayer.exe,0");
Registry.ClassesRoot.CreateSubKey(progId + @"\shell\open\command").SetValue(null, @"C:\Program Files\Windows Media Player\wmplayer.exe /open ""%1""");
}
catch
{
Console.WriteLine("An error occurred while attempting to set file associations.");
}
四、修改打开方式
除了设置默认打开方式外,createassociation函数还可以用于修改某个文件类型的打开方式。例如你想把某个视频文件的默认播放方式从Windows Media Player改为VLC Media Player,可以使用createassociation函数来实现:
string fileExtension = ".mp4";
string progId = "VLC.mp4";
try
{
Registry.ClassesRoot.CreateSubKey(fileExtension).SetValue(null, progId);
Registry.ClassesRoot.CreateSubKey(progId).CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + vlcPath + "\" \"%1\"");
}
catch
{
Console.WriteLine("An error occurred while attempting to set file associations.");
}
五、总结
createassociation是Windows操作系统中一个常用的函数,用于创建文件关联、设置默认打开方式、修改打开方式等操作。以上通过不同实例来说明createassociation的使用方法,希望能够帮助大家掌握此函数的使用。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/298078.html
微信扫一扫
支付宝扫一扫