Wpf 获取项目根路径的正确方法


在网上看到好多的朋友是使用下面的方法获取

             public static string GetProjectRootPath()
            {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string rootpath = path.Substring(0, path.LastIndexOf("\\"));
            rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\"));
            rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\"));         
            return rootpath;
           }

     这个是可以获取到,但是如果目标平台改变,生成时文件结构就会不同,有时获取不准确,所以我用下面这个方法,

           public static string GetProjectRootPath()
           {
            string path = AppDomain.CurrentDomain.BaseDirectory;         
            string rootpath = path.Substring(0, path.LastIndexOf("bin"));
            return rootpath;
            }

    不但获取准确,运算也少拉两次

猜你喜欢

转载自blog.csdn.net/badxnui/article/details/79459517
WPF