C#- WinForm获取 当前执行程序路径的几种方法

1.获取和设置当前目录的完全限定路径。
string str = System.Environment.CurrentDirectory;
Result: C:xxxxxx 


2.获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
Result: C:xxxxxx 

 

3.获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
Result: C:xxxxxxxxx.exe

 

4.获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
Result: C:xxxxxx 

 

5.获取应用程序的当前工作目录。
string str = System.IO.Directory.GetCurrentDirectory();

Result: C:xxxxxx

 

6.获取和设置包含该应用程序的目录的名称。

string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

Result: C:xxxxxx
 

7.获取当前进程的完整路径,包含文件名。

string str = this.GetType().Assembly.Location;

Result: C:xxxxxxxxx.exe

  



在dll中有时需要使用主调用程序中的资源,这就要正确获取调用程序的文件名及其路径等信息。这需要和调用dll本身的文件名和路径区分开来!
这就牵扯到System.Reflection.Assembly程序集类使用了。
GetExecutingAssembly : 获取包含当前执行的代码的程序集
GetCallingAssembly : 返回调用当前正在执行的方法的方法的 System.Reflection.Assembly

 

 转载自:http://blog.csdn.net/yanlele424/article/details/7329389

posted @   春天又来了  阅读(303)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示