「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。

-[[戻る>各種パスを取得する方法]]

* 目次 [#w864c749]
#contents

*概要 [#oe28f7bc]
調査したところ、

>[[System.AppDomain.CurrentDomain.BaseDirectory>#k18d4cb5]]

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

*方法 [#i245c7f3]

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

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

 string path = System.AppDomain.CurrentDomain.BaseDirectory

***System.Environment [#o8f5ed1f]
環境変数の current directoryを取得する場合に使用する。
-アプリケーションの実行中に変化する可能性のある値。
-OpenFileDialogでファイルを選択されたディレクトリに変更される可能性がある。

 string path = System.Environment.CurrentDirectory

**System.IO [#qa818f36]
[[System.Environment>#o8f5ed1f]]と同じ、~
環境変数の current directoryを取得する場合に使用する。

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

**System.Reflection.Assembly [#w4dc830e]
AssemblyクラスのLocationプロパティでAssemblyのパスを取得できる。

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

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

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

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

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

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

**System.Windows.Forms.Application [#oce14b8b]
Windows.Forms用

***ExecutablePath [#od02795a]
 string path = Application.ExecutablePath;

***StartupPath [#g2aaf53b]
 string path = Application.StartupPath;

*参考 [#z2b1f9f0]

-c# - Should I use AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory? - Stack Overflow~
https://stackoverflow.com/questions/674857/should-i-use-appdomain-currentdomain-basedirectory-or-system-environment-current

-c# - アプリケーションのフォルダパスを取得する最善の方法 - .net | CODE Q&A [日本語]~
https://code.i-harness.com/ja/q/5c2ef4

**.NET Tips (VB.NET,C#...) [#ma325b06]
-カレントディレクトリ(現在の作業ディレクトリ)を取得、設定する~
https://dobon.net/vb/dotnet/file/currentdirectory.html
-自分のアプリケーションの実行ファイルのパスを取得する、~
VB6のApp.Pathと同じ事を行うには?~
https://dobon.net/vb/dotnet/vb6/apppath.html

**tekkの日記 C#,VB.NET [#ja1ecae7]
-現在実行しているDLLのパスを取得する。(GetExecutingAssembly)~
http://d.hatena.ne.jp/tekk/20110307/1299513821
-アプリケーションの実行パスを取得する。どの方法が適切か検討してみた。~
http://d.hatena.ne.jp/tekk/20110222/1298371975

**[[Webサイトのパスを取得する方法]] [#x96b82d3]

----
Tags: [[:.NET開発]]

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS