C#实现获取程序路径方法小结
作者:软件工程师 时间:2022-05-09 19:40:48
本文实例讲述了C#实现获取程序路径方法。分享给大家供大家参考。具体如下:
获取DLL的目录:
Assembly myAssembly = Assembly.GetEntryAssembly();
string path = myAssembly.Location;
DirectoryInfo dr = new DirectoryInfo(path);
path=dr.Parent; //当前目录的上一级目录
C#获取程序当前路径的方法:
System.Environment.CurrentDirectory;
//例: c:\test
Application.ExecutablePath;(包括名称)
//例: c:\test\myapp.exe
Application.StartupPath;(不包括名称)
//例: c:\test
//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取和设置包含该应用程序的目录的名称。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取应用程序的当前工作目录(不可靠)。
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (.exe文件所在的目录)
获取系统特殊文件夹路径(收藏夹,桌面)
1. 收藏夹路径
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites)
2. 桌面路径
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,获取,路径
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot实现PPT格式文件上传并在线预览功能
2023-07-22 14:44:56
![](https://img.aspxhome.com/file/2023/3/57403_0s.png)
一天时间用Java写了个飞机大战游戏,朋友直呼高手
2023-12-11 10:51:30
![](https://img.aspxhome.com/file/2023/2/81892_0s.png)
深入理解C#中foreach遍历的使用方法
2023-11-02 08:51:04
![](https://img.aspxhome.com/file/2023/5/82135_0s.jpg)
C#使用ScrapySharp快速从网页采集数据
2021-08-14 09:25:14
![](https://img.aspxhome.com/file/2023/8/71728_0s.png)
java实现顺时针打印矩阵
2023-06-26 19:17:22
SpringCloud Zuul网关功能实现解析
2022-09-12 14:30:31
![](https://img.aspxhome.com/file/2023/3/67453_0s.png)
使用java + selenium + OpenCV破解腾讯防水墙滑动验证码功能
2023-07-23 14:32:47
![](https://img.aspxhome.com/file/2023/3/75653_0s.png)
SpringBoot动态修改日志级别的操作
2022-10-31 05:00:30
![](https://img.aspxhome.com/file/2023/6/67116_0s.png)
Spring Boot应用程序同时支持HTTP和HTTPS协议的实现方法
2022-01-10 16:50:43
![](https://img.aspxhome.com/file/2023/2/61952_0s.jpg)
JAVA调用SAP WEBSERVICE服务实现流程图解
2023-11-07 03:54:57
![](https://img.aspxhome.com/file/2023/2/58992_0s.png)
IntelliJ Plugin 开发之添加第三方jar的示例代码
2021-11-27 13:35:52
![](https://img.aspxhome.com/file/2023/5/77105_0s.png)
SpringCloud搭建netflix-eureka微服务集群的过程详解
2023-09-02 18:11:52
![](https://img.aspxhome.com/file/2023/3/58293_0s.jpg)
如何使用C#修改本地Windows系统时间
2022-05-06 08:23:00
![](https://img.aspxhome.com/file/2023/6/77276_0s.png)
SpringCloud消息总线Bus配置中心实现过程解析
2023-02-14 10:05:18
![](https://img.aspxhome.com/file/2023/7/64427_0s.png)
java split()使用方法解析
2023-10-18 01:59:23
![](https://img.aspxhome.com/file/2023/4/71874_0s.jpg)
c#正反序列化XML文件示例(xml序列化)
2023-01-06 02:49:57
C语言实现扫雷游戏(含注释详解)
2023-11-02 15:25:07
![](https://img.aspxhome.com/file/2023/4/107384_0s.jpg)
Android Studio gradle 编译提示‘default not found’ 解决办法
2023-07-19 09:23:06
java8中:: 用法示例(JDK8双冒号用法)
2023-11-25 06:21:21
下载软件后使用c#获取文件的md5码示例
2022-02-08 21:39:58