C#設置自界說文件圖標完成雙擊啟動(修正注冊表)。本站提示廣大學習愛好者:(C#設置自界說文件圖標完成雙擊啟動(修正注冊表))文章只能為提供參考,不一定能成為您想要的結果。以下是C#設置自界說文件圖標完成雙擊啟動(修正注冊表)正文
法式生成的自界說文件,好比後綴是.test
這類文件怎樣直接啟動翻開法式,並翻開本文件呢
1、雙擊翻開
2、自界說的文件,有圖標顯示
3、自界說的文件,點擊右鍵有響應的屬性
後台代碼:(若何在注冊表中修正信息)
//對象啟動途徑
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);
//設置默許圖標
Microsoft.Win32.RegistryKey iconKey = registryKey.CreateSubKey("DefaultIcon");
iconKey.SetValue("", System.Windows.Forms.Application.StartupPath + "\\logo.ico");
//設置默許翻開法式途徑
registryKey = registryKey.CreateSubKey("shell\\open\\command");
registryKey.SetValue("", toolPath + " %1");
//封閉
registryKey.Close();
在修正了注冊表信息後,雙擊文件是啟動了軟件,以後怎樣在代碼中操作?
//雙擊啟動翻開
//假如原有途徑中存在空格,則會分化成多個元素
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 能否有值,假如有,則獲得途徑下的文件,持續操作。
以上就是小編為年夜家整頓的C#設置自界說文件圖標完成雙擊啟動的全體內容,願望這篇文章的內容對年夜家的進修或許任務能有必定的贊助,假如有疑問可以留言交換。