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

来源:互联网 发布:社交网络发展历程 编辑:程序博客网 时间:2024/05/21 22:22
 

     示例:新建了一个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中