string str="";
str += "\r\n" + System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
str += "\r\n" + System.Environment.CurrentDirectory;
str += "\r\n" + System.IO.Directory.GetCurrentDirectory();
str += "\r\n" + System.AppDomain.CurrentDomain.BaseDirectory;
str += "\r\n" + System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str += "\r\n" + System.Windows.Forms.Application.StartupPath;
str += "\r\n" + System.Windows.Forms.Application.ExecutablePath;
Console.Write(str);
Console.ReadKey();

得到结果:

D:\路径测试\bin\Debug\路径测试.vshost.exe
D:\路径测试\bin\Debug
D:\路径测试\bin\Debug
D:\路径测试\bin\Debug\
D:\路径测试\bin\Debug\
D:\路径测试\bin\Debug
D:\路径测试\bin\Debug\路径测试.EXE

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
获取模块的完整路径。
2. System.Environment.CurrentDirectory
获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
3. System.IO.Directory.GetCurrentDirectory() 
获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,我也搞不懂了。
4. System.AppDomain.CurrentDomain.BaseDirectory
获取程序的基目录。确认这种方法在 windows服务中是可以用的
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
获取和设置包括该应用程序的目录的名称。
6. System.Windows.Forms.Application.StartupPath 
获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已
7. System.Windows.Forms.Application.ExecutablePath
获取启动了应用程序的可执行文件的路径及文件名,效果有时和1一样。

C#中得到程序当前工作目录和执行目录的五种方法

string str="";
str += "\r\n" + System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
str += "\r\n" + System.Environment.CurrentDirectory;
str += "\r\n" + System.IO.Directory.GetCurrentDirectory();
str += "\r\n" + System.AppDomain.CurrentDomain.BaseDirectory;
str += "\r\n" + System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
str += "\r\n" + System.Windows.Forms.Application.StartupPath;
str += "\r\n" + System.Windows.Forms.Application.ExecutablePath;
Console.Write(str);
Console.ReadKey();

得到结果:

D:\路径测试\bin\Debug\路径测试.vshost.exe
D:\路径测试\bin\Debug
D:\路径测试\bin\Debug
D:\路径测试\bin\Debug\
D:\路径测试\bin\Debug\
D:\路径测试\bin\Debug
D:\路径测试\bin\Debug\路径测试.EXE

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
获取模块的完整路径。
2. System.Environment.CurrentDirectory
获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
3. System.IO.Directory.GetCurrentDirectory() 
获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,我也搞不懂了。
4. System.AppDomain.CurrentDomain.BaseDirectory
获取程序的基目录。确认这种方法在 windows服务中是可以用的
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
获取和设置包括该应用程序的目录的名称。
6. System.Windows.Forms.Application.StartupPath 
获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已
7. System.Windows.Forms.Application.ExecutablePath
获取启动了应用程序的可执行文件的路径及文件名,效果有时和1一样。

C#中得到程序当前工作目录和执行目录的五种方法的更多相关文章

  1. VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  2. 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  3. JS中判断某个字符串是否包含另一个字符串的五种方法

    String对象的方法 方法一: indexOf()   (推荐) ? 1 2 var str = "123" console.log(str.indexOf("2&qu ...

  4. python中执行shell的两种方法总结

    这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...

  5. C#统计给定的文本中字符出现的次数,使用循环和递归两种方法

    前几天看了一个.net程序员面试题目,题目是”统计给定的文本中字符出现的次数,使用循环和递归两种方法“. 下面是我对这个题目的解法: 1.使用循环: /// <summary> /// 使 ...

  6. Oracle中获取执行计划的几种方法分析

    以下是对Oracle中获取执行计划的几种方法进行了详细的分析介绍,需要的朋友可以参考下     1. 预估执行计划 - Explain PlanExplain plan以SQL语句作为输入,得到这条S ...

  7. 在Activity中响应ListView内部按钮的点击事件的两种方法!!!

    在Activity中响应ListView内部按钮的点击事件的两种方法 转载:http://www.cnblogs.com/ivan-xu/p/4124967.html 最近交流群里面有人问到一个问题: ...

  8. (网页)angular中实现li或者某个元素点击变色的两种方法(转)

    转自脚本之家: 本篇文章主要介绍了angular中实现li或者某个元素点击变色的两种方法,非常具有实用价值,需要的朋友可以参考下 本文介绍了angular中实现li或者某个元素点击变色的两种方法,分享 ...

  9. SQL Server中灾难时备份结尾日志(Tail of log)的两种方法

    转自:http://www.cnblogs.com/CareySon/archive/2012/02/23/2365006.html SQL Server中灾难时备份结尾日志(Tail of log) ...

随机推荐

  1. mac 安装robot framework报错:No matching distribution found for Pywin32

    运行  pip install robotframework-ride  ,出现以下错误 解决办法:修改Python的运行方式,需要32位的python运行 defaults write com.ap ...

  2. 二维码图片以字符串的形式保存DB,已文件流显示页面上

    以下是生成二维码的方法,我只用其中一个方法 这个需要引用ZXing.DLL 链接:https://pan.baidu.com/s/1mCTwHiAm_awtsPcibAotZw 提取码:ufp6 pu ...

  3. Scala控制语句

    2019-04-16 19:03:01 if else 表达式 var sumVal = 0 if ( sumVal == 0 ) { println("true") } else ...

  4. php----------const 定义的常量和define()定义的常量的区别?

    用法一:const用于类成员变量,一经定义不可修改,define用于全局常量,不可用于类成员变量的定义,const可在类中使用也可以在类外面使用,define不能. 定义:const 常量名=值; 没 ...

  5. Unity 特写镜头

    using UnityEngine; public class Camera_Feature : MonoBehaviour { public static Camera_Feature instan ...

  6. 从拥抱开源到回馈开源,灵雀云助力CNCF中国区培训业务

    6月27日,全球首屈一指的开源盛会 2018 LinuxCon + ContainerCon + CloudOpen China (LC3)在中国北京国家会议中心落下帷幕.二度落地中国的LC3大会热度 ...

  7. elasticsearch数据备份与sshfs建立共享文件

    1.背景: 最近公司为了适应业务的发展,利用elasticsearch搜索引擎搭建了两个节点.为了防止数据丢失的特殊情况,需要定时做数据备份,而由于elasticsearch为两个节点分别在不同的服务 ...

  8. WebSphere应用程序更新方式

    在日常的使用中,一个javaee程序部署以后,以后续的使用中会有模块等的更新,但我经常见到有人直接更新服务器上的类包,文件等,(可能是tomcat,jboss这种热部署使用惯了)这样子的方式有几个问题 ...

  9. Angular4中使用后台去数据,Swiper不能滑动的解决方法(一)

      Swiper是目前较为流行的移动端触摸滑动插件,因为其简单好用易上手,很受很多设计师的欢迎. 今天在使用Swiper的时候遇到这个问题: 使用angularjs动态循环生成swiper-slide ...

  10. appium工作原理

    Appium原理 面试的时候,被问到appium原理,一点不会,实在尴尬.大家可以直接翻看原作https://blog.csdn.net/jffhy2017/article/details/69220 ...