程式生成的自定義檔案,比如字尾是.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 是否有值,如果有,則擷取路徑下的檔案,繼續操作。
作者:
唐宋元明清2188出處:
http://www.cnblogs.com/kybs0/本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須在文章頁面給出原文連接配接,否則保留追究法律責任的權利。