C#關(guān)聯(lián)自定義文件類型到應(yīng)用程序并實現(xiàn)自動導(dǎo)入功能
這幾天開發(fā)遇到一個需求:
- 將自定義文件后綴添加默認(rèn)圖標(biāo)雙擊
- 自定義的文件后綴可以自動關(guān)聯(lián)到指定的應(yīng)用程序并自動打開
上述第一、二兩點其實是一個需求,本質(zhì)是和注冊表有關(guān)的操作,在注冊表中寫入默認(rèn)圖標(biāo)、默認(rèn)文件后綴、指定打開文件,一旦明白這個思路代碼就非常清晰了。
代碼中我們寫入了兩個注冊表。
第一個結(jié)構(gòu)如下圖所示,其中DefaultIcon定義的是默認(rèn)圖標(biāo)地址,Command中存放的是指定識別的軟件的運行exe路徑。
第二個注冊表如下圖所示,將默認(rèn)文件后綴和第一個注冊表進(jìn)行關(guān)聯(lián),這樣就可以進(jìn)行識別。
添加進(jìn)去之后,我們發(fā)現(xiàn)雙擊文件可以打開軟件,但是默認(rèn)圖標(biāo)并沒有刷新,經(jīng)過查詢,需要重啟,但這樣不是太low了嗎,難道每個用戶安裝一次軟件都要重啟嗎,這時候就需要實現(xiàn)自動對注冊表進(jìn)行刷新,直接引用即可,代碼如下。
[DllImport("shell32.dll")] public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
using System; using System.Collections.Generic; using System.Windows.Forms; using Microsoft.Win32; namespace AppC { static class Program { /// <summary> /// 應(yīng)用程序的主入口點。 /// </summary> [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); /// 是否通過關(guān)聯(lián)打開的軟件 if (args.Length > 0) { string path=""; for(int i=0;i<args.Length;i++) path+=args[i]+" "; path=path.TrimEnd(' '); Console.WriteLine(path); Console.ReadKey(); //Application.Run(new mainForm(path)); } else { string keyName; string keyValue; keyName = "C2File"; keyValue = "C2文件"; RegistryKey isExCommand = null; bool isCreateRegistry = true; try { /// 檢查 文件關(guān)聯(lián)是否創(chuàng)建 isExCommand = Registry.ClassesRoot.OpenSubKey(keyName); if (isExCommand == null) { isCreateRegistry = true; } else { if (isExCommand.GetValue("Create").ToString() == Application.ExecutablePath.ToString()) { isCreateRegistry = false; } else { Registry.ClassesRoot.DeleteSubKeyTree(keyName); isCreateRegistry = true; } } } catch (Exception) { isCreateRegistry = true; } /// 假如 文件關(guān)聯(lián) 還沒有創(chuàng)建,或是關(guān)聯(lián)位置已被改變 //if (isCreateRegistry) { try { RegistryKey key, keyico; key = Registry.ClassesRoot.CreateSubKey(keyName); key.SetValue("Create", Application.ExecutablePath.ToString()); keyico = key.CreateSubKey("DefaultIcon"); keyico.SetValue("", Application.ExecutablePath + ",0"); /* string icoFind = Path.Combine("Resources", "C2", "Icon"); string icoFile = Path.Combine(System.Windows.Forms.Application.StartupPath, icoFind, "Icon.ico"); iconKey.SetValue(String.Empty, icoFile); */ key.SetValue("", keyValue); key = key.CreateSubKey("Shell"); key = key.CreateSubKey("Open"); key = key.CreateSubKey("Command"); /// 關(guān)聯(lián)的位置 key.SetValue("", Application.ExecutablePath.ToString() + @" %1/"); /// 關(guān)聯(lián)的文件擴(kuò)展名, keyName = ".c2"; keyValue = "C2File"; key = Registry.ClassesRoot.CreateSubKey(keyName); key.SetValue("", keyValue); } catch (Exception) { } } //Application.Run(new mainForm("")); } } } }
但是還有一個問題,就是打包,由于我們對注冊表的寫入是寫在Main函數(shù)里的,如果用Visual Studio自帶的工具進(jìn)行打包的話,也就是說每次用戶在打開軟件都會進(jìn)行一次注冊表讀寫的判斷,雖然用戶不感知,但是這樣并不好。所以我們并不打算將注冊表的寫入放在Main函數(shù)中,而是使用Inno Script Studio這個工具進(jìn)行美化,將注冊表的寫入放在里面,這樣用戶只會在安裝的時候?qū)懭胱员?,刪除軟件注冊表也會自動清空。
#define MyAppExeName "軟件.exe" #define AppIconName "默認(rèn)圖標(biāo)路徑" [Registry] Root:HKCU;Subkey: "{#MyRegInstallPath_sk}" ; ValueType:string; ValueName:"{#MyRegInstallPath_vn}"; ValueData:"{app}";Flags:uninsdeletekeyifempty Root: HKCR; Subkey: ".c2"; Flags: uninsdeletekey Root: HKCR; Subkey: ".c2"; ValueType: string; ValueName: ""; ValueData: "C2File" Root: HKCR; Subkey: "C2File"; Flags: uninsdeletekey Root: HKCR; Subkey: "C2File\DefaultIcon"; Flags: uninsdeletekey Root: HKCR; Subkey: "C2File\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\{#AppIconName}"; Flags: Root: HKCR; Subkey: "C2File\shell"; Flags: uninsdeletekey Root: HKCR; Subkey: "C2File\shell\open"; Flags: uninsdeletekey Root: HKCR; Subkey: "C2File\shell\open\command"; Flags: uninsdeletekey Root: HKCR; Subkey: "C2File\shell\open\command"; ValueType: string; ValueName: ""; ValueData: "{app}\{#MyAppExeName} ""%1"""; Flags:
到此這篇關(guān)于C#關(guān)聯(lián)自定義文件類型到應(yīng)用程序并實現(xiàn)自動導(dǎo)入的文章就介紹到這了,更多相關(guān)c#應(yīng)用程序自動導(dǎo)入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity的IFilterBuildAssemblies實用案例深入解析
這篇文章主要為大家介紹了Unity的IFilterBuildAssemblies實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05C#中Activator.CreateInstance()方法用法分析
這篇文章主要介紹了C#中Activator.CreateInstance()方法用法,實例分析了C#中Activator.CreateInstance()方法的功能、定義及使用技巧,需要的朋友可以參考下2015-03-03C#實現(xiàn)String類型和json之間的相互轉(zhuǎn)換功能示例
這篇文章主要介紹了C#實現(xiàn)String類型和json之間的相互轉(zhuǎn)換功能,涉及C# json格式數(shù)據(jù)的構(gòu)造、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-09-09