自分のアプリケーションの実行ファイルのパスを取得する
VB6のApp.Pathと同じ事を行うには?
VB6のApp.Pathは、自分自身のアプリケーションの実行ファイルがあるフォルダのパスを返します。ここでは、実行ファイルがあるフォルダのパスや、実行ファイルのパスを取得する方法を紹介します。
Assembly.GetExecutingAssemblyやGetEntryAssemblyメソッドを使う
MSDN によると、VB6のApp.Pathに代わるものは、「System.Reflection.Assembly.GetExecutingAssembly() .Location」とのことです。ただしこれはフォルダ名ではなく、実行ファイルのフルパスになります。よってVB6のApp.Pathと同じものということになると、次のようにPath.GetDirectoryNameメソッド でフォルダ名の部分を取得します。
Public Shared Function GetAppPath() As String
Return System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().Location)
End Function
public static string GetAppPath()
{
return System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
}
「Assembly.GetExecutingAssembly().Location」をDLL内で呼び出すと、DLLのフルパスを返します。このような場合でもEXEファイルのフルパスを取得できるようにするには、「Assembly.GetEntryAssembly() .Location」を使います。
つまり、上記の例でもDLLがあるフォルダのパスを返します。EXEファイルのあるフォルダのパスを返すようにするには、GetExecutingAssemblyの代わりにGetEntryAssemblyを使います。ただしAssembly.GetEntryAssemblyメソッドはアンマネージコードから呼び出されるとNULLを返しますので、NULLのチェックが必要です。
以下にAssembly.GetEntryAssemblyメソッドを使った例を示します。ここではGetEntryAssemblyがNULLを返した時に空白文字列を返すようにしています。
Public Shared Function GetExeAppPath() As String
Dim asm As System.Reflection.Assembly = _
System.Reflection.Assembly.GetEntryAssembly()
If asm Is Nothing Then
Return ""
Else
Return System.IO.Path.GetDirectoryName(asm.Location)
End If
End Function
public static string GetExeAppPath()
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetEntryAssembly();
if (asm == null )
return "";
else
return System.IO.Path.GetDirectoryName(asm.Location);
}
GetExecutingAssemblyとGetEntryAssembly、及び、Location、CodeBase、EscapedCodeBaseプロパティ の違いを示す例を紹介します。
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().Location)
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase)
Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().Location)
Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().CodeBase)
Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().EscapedCodeBase)
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().Location);
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase);
Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().Location);
Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
Console.WriteLine(System.Reflection.Assembly.GetEntryAssembly().EscapedCodeBase);
Application.ExecutablePathとStartupPathプロパティを使う
Windowsフォームアプリケーションでは(そうでなくても可能ですが)、アプリケーションを開始した実行ファイルのフルパスをApplication.ExecutablePathプロパティ で、アプリケーションを開始した実行ファイルのフォルダをApplication.StartupPathプロパティ で取得できます。
補足:Application.ExecutablePathは、基本的にはAssembly.GetEntryAssemblyメソッドが返すAssemblyのCodeBaseとなり、GetEntryAssemblyメソッドがNULLを返したらWin32 APIのGetModuleFileName関数の結果を返すようです。Application.StartupPathプロパティは、GetModuleFileName関数の結果からフォルダ名を返しているようです。
Console.WriteLine(System.Windows.Forms.Application.ExecutablePath)
Console.WriteLine(System.Windows.Forms.Application.StartupPath)
Console.WriteLine(System.Windows.Forms.Application.ExecutablePath);
Console.WriteLine(System.Windows.Forms.Application.StartupPath);
VB.NETで、My.Application.Info.DirectoryPathプロパティを使う
.NET Framework 2.0以降のVB.NETでは、「My.Application.Info.DirectoryPath 」によってアプリケーションのフォルダを取得できます。
補足:My.Application.Info.DirectoryPathは、基本的には「Assembly.GetEntryAssembly().Location」ですが、GetEntryAssemblyがNULLを返す時は、「
Assembly.GetCallingAssembly() .Location」です。
Console.WriteLine(My.Application.Info.DirectoryPath)
AppDomain.CurrentDomain.BaseDirectoryプロパティを使う
現在のアプリケーションドメインのベースディレクトリは、「AppDomain.CurrentDomain.BaseDirectory 」で取得できます。これは、「AppDomain.CurrentDomain.SetupInformation.ApplicationBase 」と同じです。このフォルダは大抵実行ファイルのフォルダと同じになりますが、そうでない可能性もあります。
Console.WriteLine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
Console.WriteLine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
履歴:
2011/11/22 説明を大幅に書き加えた。
注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。
Windows Vista以降でUACが有効になっていると、ファイルへの書き込みに失敗する可能性があります。詳しくは、こちら をご覧ください。