牛骨文教育服务平台(让学习变的简单)
博文笔记

C# 获取当前路径7种方法

创建时间:2017-05-22 投稿人: 浏览次数:1288

//获取模块的完整路径。  
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;  
//获取和设置当前目录(该进程从中启动的目录)的完全限定目录  
string path2 = System.Environment.CurrentDirectory;  
//获取应用程序的当前工作目录  
string path3 = System.IO.Directory.GetCurrentDirectory();  
//获取程序的基目录  
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;  
//获取和设置包括该应用程序的目录的名称  
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;  
//获取启动了应用程序的可执行文件的路径  
string path6 = System.Windows.Forms.Application.StartupPath;  
//获取启动了应用程序的可执行文件的路径及文件名  
string path7 = System.Windows.Forms.Application.ExecutablePath;

 System.IO.Directory.GetCurrentDirectory与System.Windows.Forms.Application.StartupPath的用法

  .Net Framework中System.IO.Directory.GetCurrentDirectory()方法用于获得应用程序当前工作目录。如果使用此方法获得应用程序所在的目录,应该注意:System.IO.Directory.GetCurrentDirectory()方法获得的目录路径随着OpenFileDialog、SaveFileDialog等对象所确定的目录而改变(切换工作目录)。每打开一次文件夹或者使用资源管理器查看一下文件,都会更改此方法获得的值。

而System.Windows.Forms.Application.StartupPath或System.AppDomain.CurrentDomain.BaseDirectory可以获得应用程序运行所在的目录,它是不随你打开的文件夹而变的。只跟应用程序运行目录有关,其值等于应用程序启动的根目录。例如你安装了程序在了C:Program Files程序文件夹  的位置下,那么他就是System.Windows.Forms.Application.StartupPath的值。

阅读更多
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。