Windows フォーム アプリケーションの実行ディレクトリへのパスを取得したいと考えています。(つまり、実行可能ファイルがあるディレクトリです。)
これを行うための.NETの組み込みメソッドを知っている人はいますか?
VB.NET で
Dim directory as String = My.Application.Info.DirectoryPath
C# の場合
string directory = AppDomain.CurrentDomain.BaseDirectory;
Application.Current は appdomain になります http://msdn.microsoft.com/en-us/library/system.appdomain_members.aspx
また、これにより、アセンブリの場所がわかります
AppDomain.CurrentDomain.BaseDirectory
アプリケーションの場所を取得する方法が複数あったことを思い出したようです。しかし、これは少なくとも過去に私のために働いていました(winformsプログラミングを行ってからしばらく経ちました:/)
System.Windows.Forms.Application.StartupPath
あなたの問題を解決すると思います
どちらの例も VB.NET にあります。
デバッグ パス:
TextBox1.Text = My.Application.Info.DirectoryPath
EXE パス:
TextBox2.Text = IO.Path.GetFullPath(Application.ExecutablePath)
これをチェックしてください:
Imports System.IO
Imports System.Management
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = Path.GetFullPath(Application.ExecutablePath)
Process.Start(TextBox1.Text)
End Sub
End Class
string apppath =
(new System.IO.FileInfo
(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;