代码改变世界

Directory.GetCurrentDirectory和Application.StartupPath的区别

2014-04-12 11:54  CodeCy  阅读(382)  评论(0编辑  收藏  举报

System.IO.Directory.GetCurrentDirectory()方法用于获得应用程序当前工作目录。 
System.Windows.Forms.Application.StartupPath 获得应用程序的可执行文件的路径,不含文件名 

StartupPath 可执行文件固定就不会变,可用于定位 
GetCurrentDirectory 不一定是可执行文件所在目录,因启动的位置而变 

如有两个程序 C:/A/a.exe 和 C:/B/b.exe 
当在 a.exe 中启动 b.exe,System.Diagnostics.Process.Start(@"C:/B/b.exe") 
这时,b.exe 中 Directory.GetCurrentDirectory()方法 返回值为“C:/A”,Application.StartupPath 值为“C:/B” 
如果直接在 C:/B/ 中启动 b.exe,那么两个值就会一样,都为“C:/B” 
这就是 Directory.GetCurrentDirectory 和 Application.StartupPath 的区别