当前位置:网站首页>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的值。
边栏推荐
- qml 图片预览
- 一些企业数据平台建设的思考
- VOS3000如何呼入送到OKCC
- Three cases of thread blocking.
- 【Utils】ServletUtil
- These three online PS tools should be tried
- [lvgl events] Application of events on different components (I)
- Three methods to disassemble the rotation array
- 每日一题——奖学金
- LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
猜你喜欢

Entering the world of audio and video -- flv video packaging format

Minitest -- applet automation testing framework

阿里、京东、抖音:把云推向产业心脏

RSA encrypts data with private key and decrypts data with public key (not a signature verification process)

Development and definition of software testing

QQ robot configuration record based on nonebot2

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

文献阅读(245)Roller

Clickhouse architecture and design

LeetCode 0142.环形链表 II
随机推荐
ZABBIX distributed
Multi level cache scheme
聊天室功能的实现
离散对数问题(DLP) && Diffie-Hellman问题(DHP)
QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样
Websocket chat
Install mysql5.7.36 in CentOS
数据库系统概论(第5版)补充习题——第一章 绪论
Nport serial server configuration website (whether the serial server is from network port to serial port)
What is gossip (E-Net gossip)
QQ robot configuration record based on nonebot2
数据库优化 理解这些就够了
Clickhouse architecture and design
[ecmascript6] set and map
Clickhouse distributed cluster construction
RSA用私钥加密数据公钥解密数据(不是签名验证过程)
AFNetworking速成教程
TDengine 助力西门子轻量级数字化解决方案
HCIP第十二天
[server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks