首页 > 移动平台 > 详细

Application.StartupPath和System.Environment.CurrentDirectory的区别

时间:2017-02-15 12:06:58      阅读:157      评论:0      收藏:0      [点我收藏+]

 

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就不会这样了,无论你中间打开了哪个盘的文件,启动路径都是在桌面那里,一直不会变。

 

http://stackoverflow.com/questions/1343406/environment-currentdirectory-is-yielding-unexpected-results-when-running-install

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.

 

Application.StartupPath和System.Environment.CurrentDirectory的区别

原文:http://www.cnblogs.com/chucklu/p/6400621.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!