■■■

2016年4月3日日曜日

実行ファイルのパスを取得する方法

実行ファイルのパスを取得する方法
VB.NET

Dim appPath As String = _
System.Reflection.Assembly.GetExecutingAssembly().Location

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.Windows.Forms.Application.ExecutablePath)

Console.WriteLine(System.Windows.Forms.Application.StartupPath)
Console.WriteLine(My.Application.Info.DirectoryPath)
Console.WriteLine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
Public Shared Function GetAppPath() As String
Return
System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().Location)
End Function
Public Shared Function GetAppPath2() As String
Dim
path As String = _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase

Dim u As New Uri(path)
path = u.LocalPath + Uri.UnescapeDataString(u.Fragment)
Return System.IO.Path.GetDirectoryName(path)
End Function
C#

string appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;


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.Windows.Forms.Application.ExecutablePath);

Console.WriteLine(System.Windows.Forms.Application.StartupPath);
Console.WriteLine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
public static string GetAppPath()
{
return System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
}
public static string GetAppPath2()
{
string path =
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

Uri u = new Uri(path);
path = u.LocalPath + Uri.UnescapeDataString(u.Fragment);
return System.IO.Path.GetDirectoryName(path);
}

















■■■