自定義乙個檔案格式,如.jgrass
,如何將這種檔案格式與對應的程式關聯起來?
或者,自己編寫了乙個可以開啟txt
格式的應用程式,怎麼能夠通過雙擊txt
檔案,直接開啟這個自定義程式?
基本思路是向登錄檔中寫入或修改一些值。
具體可以參見:
如何為你的 windows 應用程式關聯一種或多種檔案型別 - walterlv
e.g. 怎麼修改txt
檔案的預設開啟格式?
理論上講,有兩種實現方式。
1 修改上圖 1 中的.txt
項的預設值,將其修改為自定義的程式id,然後在登錄檔中新增自定義的程式id,已經其對應的執行程式的路徑。
2 修改txtfile
項中的預設值,直接將其路徑修改為自定義程式的路徑。
看起來 2 的修改更小,更省事。但這是有問題的。
因為txtfile
可能不止關聯了.txt
這一種檔案格式,還關聯了很多其他的格式,直接修改txtfile
中的值,可能會導致這些檔案打不開。
txtfile
這個登錄檔項,除了程式notepad.exe
的發布者可以修改之外,其他應用都不應該去修改它,此項是對修改封閉的。
而採用方式 1,只會影響.txt
這一種檔案格式的開啟方式。在登錄檔中新增自定義的程式id,這是一種擴充套件開放的修改方式。
下面是具體**。
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using microsoft.win32;
using walterlv.win32;
namespace grassdemopark.wpf2.tiny.regedit
/// /// 該執行程式所關聯檔案的型別描述
/// e.g. text document
///
public string? typename
/// /// 該執行程式所關聯檔案的型別描述
/// e.g. 乙個神奇的文字檔案
///
public string? friendlytypename
/// /// 該執行程式所關聯檔案對應的 icon
///
public string? defaulticon
/// /// 是否總是顯示指定檔案型別的副檔名
///
public bool? isalwaysshowext
/// /// 該執行程式可執行的操作/謂詞
///
public string? operation
/// /// 對應謂詞下,其執行的具體命令;僅在有效時,此值才有效
///
public string? command
/// /// 根據指定 programid,建立 的例項。
///
///
public registerprogram(string programid)
programid = programid;
}/// /// 將此副檔名註冊到當前使用者的登錄檔中
///
public void writetocurrentuser()
/// /// 將此副檔名註冊到所有使用者的登錄檔中。(程序需要以管理員身份執行)
///
public void writetoalluser()
/// /// 將此副檔名寫入到登錄檔中
///
private void writetoregistry(registryhive registryhive)
// 寫 isalwaysshowext
if (isalwaysshowext != null)
// 寫 icon
if (defaulticon != null && !string.isnullorwhitespace(defaulticon))
\\defaulticon"), defaulticon);
}// 寫 command
if (operation != null && !string.isnullorwhitespace(operation))
\\shell\\\\command"), command ?? string.empty);}}
private string buildregistrypath(string relativepath)";}
}}
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using microsoft.win32;
using walterlv.win32;
namespace grassdemopark.wpf2.tiny.regedit
/// /// 該字尾名所指示的檔案的型別
/// e.g. text/plain
/// [mime 型別 - http | mdn]( )
/// [media types]( )
///
public string? contenttype
/// /// 該字尾名所指示的檔案的感知型別
/// e.g. text
/// [perceived types (windows) | microsoft docs]( )
///
public string? perceivedtype
/// /// 該字尾名所指示的檔案關聯的預設應用程式的 programid
///
public string? defaultprogramid
/// /// 該字尾名所指示的檔案,還可以被哪些 programid 所代表的程式開啟。
///
public ilistopenwithprogramids = new list();
/// /// 根據指定副檔名,建立 的例項。
///
///
public registerfileextension(string fileextension)
if (!fileextension.startswith(".", stringcomparison.ordinal))
is not a valid file extension. it must start with \".\"",
nameof(fileextension));
}fileextension = fileextension;
}/// /// 將此副檔名註冊到當前使用者的登錄檔中
///
public void writetocurrentuser()
/// /// 將此副檔名註冊到所有使用者的登錄檔中。(程序需要以管理員身份執行)
///
public void writetoalluser()
/// /// 將此副檔名寫入到登錄檔中
///
private void writetoregistry(registryhive registryhive)
// 寫 perceivedtype
if (perceivedtype != null && !string.isnullorwhitespace(perceivedtype))
// 寫 openwithprogramids
if (openwithprogramids.count > 0)
\\openwithprogids"), programid, string.empty);}}
}private string buildregistrypath(string relativepath)";}
}}
Linux檔案型別與副檔名
我們提過乙個概念,就是任何裝置在linux中都是檔案,不僅如此,連資料通訊的介面也由專屬檔案負責。所以,你會了解到,linux的檔案種類很多。除了前面提到的 d即所謂的一般檔案與目錄檔案之外,還有哪些種類的檔案呢?檔案種類 在介紹屬性時,提到了最前面的標誌 d或 可以表示目錄或檔案,那就是不同的檔案...
linux 檔案型別與副檔名
linux檔案型別和linux檔案的檔名所代表的意義是兩個不同的概念。我們通過一般應用程式而建立的比如file.txt file.tar.gz 這些檔案雖然要用不同的程式來開啟,但放在linux檔案型別中衡量的話,大多是常規檔案 也被稱為普通檔案 我們用ls lh來檢視某個檔案的屬性,可以看到有類似...
linux下檔案型別 副檔名
在linux檔案系統中,主要有以下幾種型別的檔案 普通檔案 regular file 就是一般型別的檔案,當用 ls l 檢視某個目錄時,第乙個屬性為 的檔案就是正規檔案,或者叫普通檔案。正規檔案又可分成純文字檔案 ascii 和二進位制檔案 binary 純文字檔案是可以通過cat,more,le...