C#文件路径操作详细总结
时间:2021-06-06 04:07:41
一、获取当前文件的路径
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\,有时不一定返回什么东东,这是任何应用程序最后一次操作过的目录,比如你用Word打开了E:\doc\my.doc这个文件,此时执行这个方法就返回了E:\doc了。
4. System.AppDomain.CurrentDomain.BaseDirectory
获取程序的基目录。
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
获取和设置包括该应用程序的目录的名称。
6. System.Windows.Forms.Application.StartupPath
获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已
7. System.Windows.Forms.Application.ExecutablePath
获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。
二、操作环境变量
利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系统环境变量,如:
System.Environment.GetEnvironmentVariable("windir")就可以取得windows系统目录的路径。
以下是一些常用的环境变量取值:
System.Environment.GetEnvironmentVariable("windir");
System.Environment.GetEnvironmentVariable("INCLUDE");
System.Environment.GetEnvironmentVariable("TMP");
System.Environment.GetEnvironmentVariable("TEMP");
System.Environment.GetEnvironmentVariable("Path");
最后贴出我进行上面操作获得的变量值,事先说明,本人是编写了一个WinForm程序,项目文件存放于D:\Visual Studio Projects\MyApplication\LifeAssistant,编译后的文件位于D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug,最后的结果如下:
1、 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\LifeAssistant.exe
2、System.Environment.CurrentDirectory=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug
3、System.IO.Directory.GetCurrentDirectory()=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug
4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\
5、 System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\
6、System.Windows.Forms.Application.StartupPath=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug
7、 System.Windows.Forms.Application.ExecutablePath=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\LifeAssistant.exe
System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS
System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\jdk1.5.0\bin;C:\MySQLServer5.0\bin;C:\Program Files\Symantec\pcAnywhere\;C:\Program Files\Microsoft SQL Server\80\Tools\BINN
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Gradle项目中的资源正确获取方式
![](https://img.aspxhome.com/file/2023/8/70568_0s.jpg)
Java微信公众号开发之通过微信公众号获取用户信息
![](https://img.aspxhome.com/file/2023/3/88043_0s.png)
SpringBoot 使用log4j2的配置过程
Android中使用CircleImageView和Cardview制作圆形头像的方法
![](https://img.aspxhome.com/file/2023/2/92582_0s.gif)
java数据结构之java实现栈
SpringBoot使用Thymeleaf自定义标签的实例代码
Java自定义异常类的实例详解
mybatis plus使用redis作为二级缓存的方法
![](https://img.aspxhome.com/file/2023/0/63150_0s.png)
Android实现3D层叠式卡片图片展示
![](https://img.aspxhome.com/file/2023/4/110744_0s.gif)
一文告诉你为什么要重写hashCode()方法和equals()方法
Kotlin使用静态变量与静态方法详解
c#项目将dll打包到exe中的步骤
IntelliJ IDEA 2022.2 正式发布新功能体验
![](https://img.aspxhome.com/file/2023/7/95947_0s.png)
C#学习笔记——基本语法
![](https://img.aspxhome.com/file/2023/7/81107_0s.png)
Java中BigDecimal的舍入模式解析(RoundingMode)
Android Studio中生成aar文件及本地方式使用aar文件的方法
![](https://img.aspxhome.com/file/2023/4/90184_0s.png)
Java基础之堆内存溢出的解决
![](https://img.aspxhome.com/file/2023/8/59338_0s.png)
C#生成随机ArrayList的方法
java split用法详解及实例代码
spring中的注解事务演示和添加步骤详情
![](https://img.aspxhome.com/file/2023/3/77983_0s.png)