.net中的目录
System.Environment.CurrentDirectory
Application.StartupPath
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath(v=vs.110).aspx
System.Environment.CurrentDirectory
https://msdn.microsoft.com/en-us/library/system.environment.currentdirectory(v=vs.110).aspx
百度之后,找到区别
System.Environment.CurrentDirectory的含义是获取或设置当前工作路径,
而Application.StartupPath是获取程序启动路径,
表面上看二者没什么区别,但实际上区别大得很。
先说前者:比如说你程序放在桌面上启动,但是中间你用了一个OpenFileDialog打开了E盘名为abc的文件夹下的某一个文件,那么CurrentDirectory就变成E:\abc了,
所以如果你想再获取程序启动文件夹的某一个文件就没用了,
但是Application.StartupPath就不会这样了,无论你中间打开了哪个盘的文件,启动路径都是在桌面那里,一直不会变。
If you want to get the path to the directory under which your executable runs, you should not rely on the Environment.CurrentDirectory,
since it can be changed in a number of ways (shotrtcut settings, etc). Try one of these options instead:
http://stackoverflow.com/questions/1321628/environment-currentdirectory-in-c-net
You shouldn't be using the Environment.CurrentDirectory value as a base for file lookups because it can change and may not always be under your control. e.g. a File Save As to a different folder may change the 'current folder' value. As you can see it can yield unpredictable results.
Use a value that you can control better. e.g. a ResourcesFolderPath value in a configuration (xml?) file that is updated when you install your app.
Desktop 和 DesktopDirectory
https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx
Desktop :The logical Desktop rather than the physical file system location.
DesktopDirectory:he directory used to physically store file objects on the desktop.
What's the difference between SpecialFolder.Desktop and SpecialFolder.DesktopDirectory?
A directory is a location in the file system.
A folder is a location in the shell namespace.
A directory is a kind of folder.
A virtual folder is not necessarily backed by a directory.
For example consider libraries or search folders.
The user's desktop directory is a location in the file system.
The desktop folder merges that with virtual items like all users items, recycle bin, shortcut to documents folder etc.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了