程序生成的自定義文件,比如后綴是.test
這種文件怎么直接啟動打開程序,并打開本文件呢
1、雙擊打開
2、自定義的文件,有圖標(biāo)顯示
3、自定義的文件,點(diǎn)擊右鍵有相應(yīng)的屬性
后臺代碼:(如何在注冊表中修改信息)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //工具啟動路徑 string toolPath = System.Windows.Forms.Application.StartupPath + "\\郵件小工具.exe" ; string extension = SptdConst.FileExtension; string fileType = "Email File" ; string fileContent = "text/plain" ; //獲取信息 Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension); if (registryKey != null && registryKey.OpenSubKey( "shell" ) != null && registryKey.OpenSubKey( "shell" ).OpenSubKey( "open" ) != null && registryKey.OpenSubKey( "shell" ).OpenSubKey( "open" ).OpenSubKey( "command" ) != null ) { var varSub = registryKey.OpenSubKey( "shell" ).OpenSubKey( "open" ).OpenSubKey( "command" ); var varValue = varSub.GetValue( "" ); if (Object.Equals(varValue, toolPath + " %1" )) { return ; } } //刪除 Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(extension, false ); //文件注冊 registryKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension); registryKey.SetValue( "文件類型" , fileType); registryKey.SetValue( "Content Type" , fileContent); //設(shè)置默認(rèn)圖標(biāo) Microsoft.Win32.RegistryKey iconKey = registryKey.CreateSubKey( "DefaultIcon" ); iconKey.SetValue( "" , System.Windows.Forms.Application.StartupPath + "\\logo.ico" ); //設(shè)置默認(rèn)打開程序路徑 registryKey = registryKey.CreateSubKey( "shell\\open\\command" ); registryKey.SetValue( "" , toolPath + " %1" ); //關(guān)閉 registryKey.Close(); |
在修改了注冊表信息后,雙擊文件是啟動了軟件,之后怎么在代碼中操作?
1 2 3 4 5 6 7 8 9 10 11 | //雙擊啟動打開 //如果原有路徑中存在空格,則會分解成多個元素 if (e.Args.Length > 0) { string filePath = String.Join( " " , e.Args.ToArray()); FileInfo file = new FileInfo(filePath); if (file.Exists) { EmailToolConst.DoubleClickSptdFilePath = file.FullName; } } |
然后可以在主程序loaded
方法中,判斷DoubleClickSptdFilePath
是否有值,如果有,則獲取路徑下的文件,繼續(xù)操作。
以上就是小編為大家整理的C#設(shè)置自定義文件圖標(biāo)實現(xiàn)雙擊啟動的全部內(nèi)容,希望這篇文章的內(nèi)容對大家的學(xué)習(xí)或者工作能有一定的幫助,如果有疑問可以留言交流。