概要
Windows 10で拡張子の関連付け情報を得るにはHKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
キーを確認する必要があります。それぞれ登録される情報は異なりますが、重複する場合はFileExts
キーの情報が優先されます。
.ps1の具体例
Windows 10でWindows PowerShell 1.0、PowerShell 7の順でインストールすると、.ps1は通常の設定ではHKEY_CLASSES_ROOT
キーではWindows PowerShell 1.0、FileExts
キーではPowerShell 7に関連付けられます。シェルはFileExts
キーを優先するため、.ps1ファイルを開くとPowerShell 7で実行されます。
上記のレジストリの構成抜粋を示します。
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1] @="Microsoft.PowerShellScript.1" [HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command] @="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'" [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice] "ProgId"="Applications\pwsh.exe" [HKEY_CLASSES_ROOT\Applications\pwsh.exe\shell\open\command] @="C:\Program Files\PowerShell\7\pwsh.exe" "%1"
Win32 APIで拡張子の関連付けを取得する方法
レジストリを直接操作すると複雑で互換性も気になりますが、Win32 APIを使用することでそれらを解決できます。以下の関数やインターフェイスが使用できます。
目的 | 関数 | DLL |
---|---|---|
関連付けの取得 | FindExecutableA / FindExecutableW | Shell32.dll |
関連付けの取得 | AssocQueryStringA / AssocQueryStringW | Shlwapi.dll |
関連付けの実行 | ShellExecuteA / ShellExecuteW | Shell32.dll |
関連付けの実行 | ShellExecuteExA / ShellExecuteExW | Shell32.dll |
関連付けの管理 | IQueryAssociations | Shlwapi.dll |