c# 中 获取应用程序的路径

来源:互联网 发布:我的女友是警花知君 编辑:程序博客网 时间:2024/05/22 09:43
 

     示例:新建了一个windows窗体应用程序WindowsFormsApplication4,保存在F:\Visual Studio 2008\Projects,启动程序在F:\VisualStudio2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug中

 

一、获得应用程序的可执行文件的路径

1. System.Windows.Forms.Application.StartupPath

    public static string StartupPath { get; }

    获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

   本例的结果为F:\VisualStudio2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug

2. System.Windows.Forms.Application.ExecutablePath

    public static string ExecutablePath { get; }

    获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

     本例的结果为:F:\VisualStudio2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\Debug\ WindowsFormsApplication4.exe

 

二、获取应用程序的当前工作目录

3.   System.IO.Directory.GetCurrentDirectory()

      public static string GetCurrentDirectory ()

      获取应用程序的当前工作目录。这个不一定是应用程序的可执行文件所在的目录。未进行任何操作前,默认状态下应用程序的当前工作目录为应用程序的可执行文件所在的目录。若通过属性vironment.CurrentDirectory改变了当前目录,那么此方法返回通过属性vironment.CurrentDirectory设置的当前目录。若在该应用程序中通过打开对话框选择了某一目录,那么该方法返回打开对话框选择的目录。即该方法返回该应用程序最后一次操作过的目录。

4.   System.Environment.CurrentDirectory

      public static string CurrentDirectory { get; set; }

      获取和设置应用程序的当前工作目录。说明同3.

 

 

     注意:若在程序中使用相对路径,那么默认保存在应用程序的当前工作目录下。例如:当前工作目录为:c:\temp 在程序中使用了相对路径“image.bmp”,那么此图像的位置为:c:\temp\image.bmp中

 

//可获得当前执行的exe的文件名。      
string str1 =Process.GetCurrentProcess().MainModule.FileName;
// 获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
//备注:按照定义,如果该进程在本地或网络驱动器的根目录中启动,
//则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:\”)。
//如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如“C:\mySubDirectory”)。  
string str2=Environment.CurrentDirectory;
//获取应用程序的当前工作目录。  
string str3=Directory.GetCurrentDirectory();
//获取基目录,它由程序集冲突解决程序用来探测程序集。  
string str4=AppDomain.CurrentDomain.BaseDirectory;
//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。  
string str5=Application.StartupPath;
//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。  
string str6=Application.ExecutablePath;
//获取或设置包含该应用程序的目录的名称。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase
原创粉丝点击