当前位置:网站首页>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的值。
边栏推荐
- Recommended super easy-to-use mobile screen recording software
- Development and definition of software testing
- [try to hack] hfish honeypot deployment
- Leetcode 0143. rearrange linked list
- 如何有效进行回顾会议(上)?
- How to effectively conduct the review meeting (Part 1)?
- Node文件操作
- RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
- redis哨兵机制
- Three cases of thread blocking.
猜你喜欢

手机滚动截屏软件推荐

QQ robot configuration record based on nonebot2

2022高处安装、维护、拆除考试题库及在线模拟考试

多所“双一流”大学,保研预报名启动!

Mobile phone scrolling screenshot software recommendation

2022年熔化焊接与热切割考题及在线模拟考试

As a programmer, how to manage time efficiently?

草料二维码--在线二维码生成器

Alibaba, jd.com, Tiktok: push cloud to the heart of industry

MVC模型:日历系统
随机推荐
UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing
QQ robot configuration record based on nonebot2
Minitest -- applet automation testing framework
Development and definition of software testing
RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
【翻译】如何为你的私有云选择一个网络网关
[util] redis tool class: change the value serializer of redis to genericjackson2jsonredisserializer, and the return value can be object or collection
Clickhouse架构与设计
深度学习基础----GNN谱域和空域 (不断完善更新积累)
Clickhouse distributed cluster construction
工厂模式和构造函数模式
什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
[线程安全问题] 多线程到底可能会带来哪些风险?
(function(global,factory){
Solve the problem that uniapp wechat applet canvas cannot introduce fonts
Forage QR code -- online QR code generator
MySQL开发技巧——视图
Clickhouse architecture and design
数据库优化 理解这些就够了
Diablo 4 ps4/ps5 beta has been added to the Playstation database