C# 获取exe当前运行目录的5种方法和区别
string str1 = Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。 string str2 = Environment.CurrentDirectory;//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。(备注:按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:\”)。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径[如“C:\mySubDirectory”])。 string str3 = Directory.GetCurrentDirectory(); //获取应用程序的当前工作目录。 string str4 = AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。 string str5 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。 WriteLog("str1:" + str1); WriteLog("str2:" + str2); WriteLog("str3:" + str3); WriteLog("str4:" + str4); WriteLog("str5:" + str5);
双击exe直接打开:
2021/10/20 15:26:55 str1:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\KafkaReceive.exe 2021/10/20 15:26:55 str2:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive 2021/10/20 15:26:55 str3:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive 2021/10/20 15:26:55 str4:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\ 2021/10/20 15:26:55 str5:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\
在MVC项目中,使用微软cmd命令模块的方式打开:
2021/10/20 15:26:01 str1:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\KafkaReceive.exe 2021/10/20 15:26:01 str2:c:\windows\system32\inetsrv 2021/10/20 15:26:01 str3:c:\windows\system32\inetsrv 2021/10/20 15:26:01 str4:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\ 2021/10/20 15:26:01 str5:C:\Hongye\HYEMS_CMS\Content\Appliction\KafkaReceive\