C#中获取程序路径的几种方法及启动方式
在开发的程序中使用自定义的配置文件属于基本的操作了,但最近发现一个问题:如果程序是由用户通过exe文件直接打开的,获取同目录下配置文件不会有问题;
但如果程序是由第三方应用启动的,通过不同方式获取的“当前目录”会有不同。
整理的几种获取当前目录的方式
及官方文档中的注释:
1 2 3 4 5 6 7 8 | string str1=Process.GetCurrentProcess().MainModule.FileName; //获取模块的完整路径。 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; //获取或设置包含该应用程序的目录的名称。 string str8= this .GetType().Assembly.Location; //获取包含清单的已加载文件的路径或UNC位置。 |
分别通过手动启动和第三方调用的方法启动应用程序(exe文件路径:D:\test\程序启动路径.exe),其值分别为:
手动启动
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序启动路径.exe |
Environment.CurrentDirectory | D:\test |
Directory.GetCurrentDirectory() | D:\test |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序启动路径.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序启动路径.exe |
通过Win+R打开运行,输入程序路径,或通过bat批处理命令运行,效果与直接打开一致。
C#使用System.Diagnostics.Process.Start()方法启动
(调用方路径:E:\test)
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序启动路径.exe |
Environment.CurrentDirectory | E:\test |
Directory.GetCurrentDirectory() | E:\test |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序启动路径.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序启动路径.exe |
通过cmd命令行启动
(cmd命令中直接输入 D:\test\程序启动路径.exe)
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序启动路径.exe |
Environment.CurrentDirectory | C:\Users\用户名 |
Directory.GetCurrentDirectory() | C:\Users\用户名 |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序启动路径.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序启动路径.exe |
通过LabVIEW中“执行系统命令”(System Exec.vi)启动
Process.GetCurrentProcess().MainModule.FileName | D:\test\程序启动路径.exe |
Environment.CurrentDirectory | C:\WINDOWS\system32 |
Directory.GetCurrentDirectory() | C:\WINDOWS\system32 |
AppDomain.CurrentDomain.BaseDirectory | D:\test\ |
Application.StartupPath | D:\test |
Application.ExecutablePath | D:\test\程序启动路径.exe |
AppDomain.CurrentDomain.SetupInformation.ApplicationBase | D:\test\ |
this.GetType().Assembly.Location | D:\test\程序启动路径.exe |
此方法在不同电脑中表现不一致,没有对比是操作系统原因还是LabVIEW原因。
在同事电脑上,Environment.CurrentDirectory和Directory.GetCurrentDirectory()方式获取的路径为 C:\Users\用户名\Desktop
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
出处:https://www.jb51.net/program/29207451c.htm
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/18388833
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
2021-08-30 快速删除C#项目中的bin和obj文件夹的方法
2017-08-30 儿童二类疫苗是否有必要接种
2017-08-30 Bootstrap组件福利篇:十二款好用的组件推荐
2013-08-30 JS页面打印指定DIV区域功能
2013-08-30 JS 网页键盘钩子
2012-08-30 JS效果的进度条