[C#-ファイルシステム] 既定のブラウザ/メーラの実行ファイルパスを取得
既定のブラウザ/メーラの.EXEファイル・パスを取得するには?[C#、VB]より、ソースコードをコピーしています。 以下のソースコードでは、コンソール用アプリケーションのコードで、実行すると規定のブラウザとメーラーのそれぞれのPATHを取得するメソッドを作成し、それらのメソッドでPATHを取得し、規定のブラウザとメーラーを起動します。
using System; using Microsoft.Win32; using System.IO; using System.Diagnostics; class Program { static void Main(string[] args) { // 既定のブラウザとメーラのパスを取得する string browserPath = GetDefaultBrowserExePath(); string mailerPath = GetDefaultMailerExePath(); // ブラウザとメーラを立ち上げる Process.Start(browserPath); Process.Start(mailerPath); } private static string GetDefaultBrowserExePath() { return _GetDefaultExePath(@"http\shell\open\command"); } private static string GetDefaultMailerExePath() { return _GetDefaultExePath(@"mailto\shell\open\command"); } private static string _GetDefaultExePath(string keyPath) { string path = ""; // レジストリ・キーを開く // 「HKEY_CLASSES_ROOT\xxxxx\shell\open\command」 RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(keyPath); if (rKey != null) { // レジストリの値を取得する string command = (string)rKey.GetValue(String.Empty); if (command == null) { return path; } // 前後の余白を削る command = command.Trim(); if (command.Length == 0) { return path; } // 「"」で始まる長いパス形式かどうかで処理を分ける if (command[0] == '"') { // 「"〜"」間の文字列を抽出 int endIndex = command.IndexOf('"', 1); if (endIndex != -1) { // 抽出開始を「1」ずらす分、長さも「1」引く path = command.Substring(1, endIndex - 1); } } else { // 「(先頭)〜(スペース)」間の文字列を抽出 int endIndex = command.IndexOf(' '); if (endIndex != -1) { path = command.Substring(0, endIndex); } else { path = command; } } } return path; } }
「C#」に関する「本」の商品を自動的に表示しています。
キーワードに関連していない商品は、Amazonがオススメする商品です。気になる物があればどうぞ。
キーワードに関連していない商品は、Amazonがオススメする商品です。気になる物があればどうぞ。
作成日:2008年06月04日(Wed) / 更新日:2008年11月23日(Sun)