一、創建桌面快捷方式
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/zh-tw/n/298078.html