项目中经常用到获取程序跟目录下的某一个Confing文件或者某个程序exe文件。
代码:下面是把获取到的路径加换行显示到WinForm的RichTextBox中
private void button1_Click(object sender, EventArgs e)
{
//获取根目录的\bin\Debug\路径
this.richTextBox1.Text = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\n";
this.richTextBox1.Text += System.AppDomain.CurrentDomain.BaseDirectory + "\n";
this.richTextBox1.Text += System.Environment.CurrentDirectory + "\n";
this.richTextBox1.Text += System.IO.Directory.GetCurrentDirectory() + "\n";
this.richTextBox1.Text += System.Windows.Forms.Application.StartupPath + "\n";
//获取\bin\Debug\下的程序.exe
this.richTextBox1.Text += System.Windows.Forms.Application.ExecutablePath + "\n";
}
用Winform做的一个点击按钮显示效果。