当前位置:网站首页>C# 获取当前路径7种方法
C# 获取当前路径7种方法
2022-07-28 13:31:00 【CDamogu】
获取当前路径7种方法


代码
//获取模块的完整路径。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//获取和设置当前目录(该进程从中启动的目录)的完全限定目录
string path2 = System.Environment.CurrentDirectory;
//获取应用程序的当前工作目录
string path3 = System.IO.Directory.GetCurrentDirectory();
//获取程序的基目录
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
//获取和设置包括该应用程序的目录的名称
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//获取启动了应用程序的可执行文件的路径
string path6 = System.Windows.Forms.Application.StartupPath;
//获取启动了应用程序的可执行文件的路径及文件名
string path7 = System.Windows.Forms.Application.ExecutablePath;
StringBuilder str=new StringBuilder();
str.AppendLine("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:" + path1);
str.AppendLine("System.Environment.CurrentDirectory:" + path2);
str.AppendLine("System.IO.Directory.GetCurrentDirectory():" + path3);
str.AppendLine("System.AppDomain.CurrentDomain.BaseDirectory:" + path4);
str.AppendLine("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + path5);
str.AppendLine("System.Windows.Forms.Application.StartupPath:" + path6);
str.AppendLine("System.Windows.Forms.Application.ExecutablePath:" + path7);
string allPath = str.ToString();
输出结果
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.vshost.exe
System.Environment.CurrentDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.IO.Directory.GetCurrentDirectory():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.AppDomain.CurrentDomain.BaseDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.Windows.Forms.Application.StartupPath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.Windows.Forms.Application.ExecutablePath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.EXE
用法须知
- Net Framework中System.IO.Directory.GetCurrentDirectory()方法用于获得应用程序当前工作目录。如果使用此方法获得应用程序所在的目录,应该注意:System.IO.Directory.GetCurrentDirectory()方法获得的目录路径随着OpenFileDialog、SaveFileDialog等对象所确定的目录而改变(切换工作目录)。每打开一次文件夹或者使用资源管理器查看一下文件,都会更改此方法获得的值。
- 而System.Windows.Forms.Application.StartupPath或System.AppDomain.CurrentDomain.BaseDirectory可以获得应用程序运行所在的目录,它是不随你打开的文件夹而变的。只跟应用程序运行目录有关,其值等于应用程序启动的根目录。例如你安装了程序在了C:\Program Files\程序文件夹 的位置下,那么他就是System.Windows.Forms.Application.StartupPath的值。
边栏推荐
- HCIP第十二天
- Nport serial server configuration website (whether the serial server is from network port to serial port)
- The default storage engine after MySQL 5.5 is InnoDB.
- unittest执行runTestCase提示<_io.TextIOWrapper name=‘<stderr>‘ mode=‘w‘ encoding=‘utf-8‘>解决方案
- Multi level cache scheme
- These three online PS tools should be tried
- 多级缓存方案
- webSocket聊天
- 【翻译】盐业公司来Linkerd公司是为了负载平衡,留下来是为了效率、可靠性和性能。...
- bgp实验
猜你喜欢

Cv:: mat conversion to qimage error

Xcode编写SwiftUI代码时一个编译通过但导致预览(Preview)崩溃的小陷阱

Langjing Technology (Trax China) "robot +ai" opens the era of Chinese retail meta universe

Leetcode 0142. circular linked list II

What is a spin lock? A spin lock means that when a thread attempts to acquire a lock, if the lock has been occupied by other threads, it will always cycle to detect whether the lock has been released,

Thesis study -- masked generative disintegration

Development and definition of software testing

复制excel行到指定行

IP black and white list

redis哨兵机制
随机推荐
用友BIP CRM新品发布,赋能大中型企业营销增长
Thrift 序列化协议浅析
Forage QR code -- online QR code generator
QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
[ecmascript6] proxy and reflection
为什么jq的匿名函数 外部可以访问到里面的方法
如何有效进行回顾会议(上)?
Thesis study -- masked generative disintegration
什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
Metersphere -- Open Source continuous testing platform
阿里、京东、抖音:把云推向产业心脏
Solve the problem that uniapp wechat applet canvas cannot introduce fonts
How does vos3000 send incoming calls to okcc
Clickhouse architecture and design
jenkins
手机滚动截屏软件推荐
Database optimization understanding these is enough
QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样
JMeter installation tutorial and login add token
2022高处安装、维护、拆除考试题库及在线模拟考试