マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。

目次

概要

調査したところ、

System.AppDomain.CurrentDomain.BaseDirectory

が推奨方法の既定値であるもよう。

方法

System

System名前空間のライブラリを使用するのが一般的。

System.AppDomain?

アプリケーションと同じディレクトリにあるファイルを検索する場合に使用する。

string path = System.AppDomain.CurrentDomain.BaseDirectory

System.Environment

環境変数の current directoryを取得する場合に使用する。

  • アプリケーションの実行中に変化する可能性のある値。
  • OpenFileDialog?でファイルを選択されたディレクトリに変更される可能性がある。
string path = System.Environment.CurrentDirectory

System.IO

System.Environmentと同じ、
環境変数の current directoryを取得する場合に使用する。

string path = System.IO.Directory.GetCurrentDirectory();

System.Reflection.Assembly

AssemblyクラスのLocationプロパティでAssemblyのパスを取得できる。

GetEntryAssembly?

EntryAssembly?メソッドでは、EXEなどのエントリポイントとなるアセンブリを取得する。

string path = System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetEntryAssembly().Location;

GetExecutingAssembly?

ExecutingAssembly?メソッドでは、実行中のコードを含むEXEやDLLのアセンブリを取得する。

string path = System.Reflection.Assembly.GetExecutingAssembly().Location

GetCallingAssembly?

GetCallingAssembly?メソッドでは、現在実行中のメソッドを呼び出したコードを含むEXEやDLLのアセンブリを取得する。

string path = System.Reflection.Assembly.GetCallingAssembly().Location

System.Windows.Forms.Application

Windows.Forms用

ExecutablePath?

string path = Application.ExecutablePath;

StartupPath?

string path = Application.StartupPath;

参考

.NET Tips (VB.NET,C#...)

tekkの日記 C#,VB.NET

Webサイトのパスを取得する方法


Tags: :.NET開発


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-12-17 (月) 17:17:30 (1950d)