satocchiaブログ
2026
2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
1月
2月
3月
4月
5月
6月
7月
8月
9月
10月
11月
12月
全1件 (1件中 1-1件目)
1
拡張子から既定のアプリケーションを探す。 簡単そうに見えて意外に難しいんです。 レジストリ、みれば簡単じゃん、って思ったあなた、甘い、甘い。 そもそも、拡張子とアプリケーションの関連付けはWindowsのバージョンアップに伴って、いろいろ変わってきています。 特に、最近は、セキュリティ上の理由から、ユーザーがGUIで設定しないといけないようになっていて、一筋縄ではいきません。 じゃ、どうするか。 その拡張子のダミーファイルを作って、起動しちゃえばいいじゃん。 なので、作ってみました。get-DefaultApplication.ps1function get-DefaultApplication{ param( [parameter(mandatory=$true)] [string]$extension ) if( $extension[0] -ne '.' ){ $extension=".$extension" } $Dummy=join-path $env:TEMP "dummy$extension" $BeforeStart=[datetime]::Now "">$Dummy try{ start-process $Dummy }catch{ if( $error -match "この操作に対して指定されたファイルには、アプリケーションが関連付けられていません" ){ rm $Dummy return "NotAssociated" } } $newerProcesses=gwmi win32_process|?{ $_.ConvertToDatetime($_.CreationDate) -gt $BeforeStart } if( $pr=($newerProcesses|?{ $_.name -eq "openwith.exe" }) ){ $pr|%{$_.terminate()}|out-null rm $Dummy return "NotAssociated" } if ( $newerProcesses.gettype().name -match "\[\]$" ){ $oldest=$null $newerProcesses|%{ if (!$oldest){ $oldest=$_ return } if( $_.ConvertToDatetime($_.CreationDate) -lt $oldest.ConvertToDatetime($oldest.CreationDate) ){ $oldest=$_ return } } $newerProcesses=$oldest } $newerProcesses.terminate()|out-null rm $Dummy $newerProcesses}<# 拡張子HTMを調べてみる。get-DefaultApplication htm#>
2019.07.22
コメント(0)