获取项目相关路径方法

解决方案
Serializer 

启动项目
WPFApplication
获取正在运行程序的完整路径

//结果 "D:\用户目录\我的文档\Visual Studio 2013\Projects\Serializer\WPFApplication\bin\Debug\WPFApplication.exe" string a = System.Reflection.Assembly.GetEntryAssembly().Location;
//结果 '\' char c = System.IO.Path.DirectorySeparatorChar; //结果 76 int b =System.Reflection.Assembly.GetEntryAssembly().Location.LastIndexOf(System.IO.Path.DirectorySeparatorChar);

获取运行程序所在文件夹的路径
//结果 "D:\用户目录\我的文档\Visual Studio 2013\Projects\Serializer\WPFApplication\bin\Debug" string appDir = System.IO.Path.Combine(System.Reflection.Assembly.GetEntryAssembly().Location.Substring(0, System.Reflection.Assembly.GetEntryAssembly().Location.LastIndexOf(System.IO.Path.DirectorySeparatorChar)));

拼接文件夹
//结果 "D:\用户目录\我的文档\Visual Studio 2013\Projects\Serializer\WPFApplication\bin\Update" string updateFileDir = System.IO.Path.Combine(System.IO.Path.Combine(appDir.Substring(0, appDir.LastIndexOf(System.IO.Path.DirectorySeparatorChar))), "Update");

获得运行程序的名字 去掉exe
//结果 "WPFApplication" string _callExeName = System.Reflection.Assembly.GetEntryAssembly().Location.Substring(System.Reflection.Assembly.GetEntryAssembly().Location.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1).Replace(".exe", ""); }

作者:zscmj
出处:http://www.cnblogs.com/zscmj/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/zscmj/p/4609081.html