/// <summary>
        /// 加载解决方案项目工程信息
        /// </summary>
        /// <param name="slnFilePath">sln文件绝对路径地址</param>
        /// <returns></returns>
        public SolutionInfo LoadSlnProjectInfo(string slnFilePath)
        {
            try
            {
                /*
                 * 一段project节点信息
                 * Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benlai.Tool.Service", "Benlai.Tool.Service\Benlai.Tool.Service.csproj", "{05F22A2C-64E6-4E6E-BE56-35E007993CF4}"
                 * EndProject
                 */
                slnFilePath = Path.GetFullPath(slnFilePath);
                var slnContent = File.ReadAllText(slnFilePath);
                if (string.IsNullOrWhiteSpace(slnContent))
                {
                    throw new Exception($"读取文件信息为空,请检查文件是否存在!文件路径:{slnFilePath}");
                }
                var dirPath = Path.GetDirectoryName(slnFilePath);
                List <SolutionProjectInfo> projList = new List <SolutionProjectInfo>();
                var projectReg               = new Regex(AppConst.Exp_ProjNode, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var projectContexts          = projectReg.Matches(slnContent); //正则匹配sln文件中的 project节点信息
                SolutionProjectInfo projInfo = null;
                foreach (var projectContext in projectContexts)
                {
                    var projectContextArr = projectContext.ToString().Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    if (projectContextArr == null || projectContextArr.Length != 2)
                    {
                        continue;
                    }
                    var projectTypeGuid = new Regex(AppConst.Exp_ProjGuid, RegexOptions.Compiled | RegexOptions.IgnoreCase).Match(projectContextArr[0].Trim()).Value;
                    if (projectTypeGuid.ToUpper() != AppConst.CSharpProjGUID_WIN)
                    {
                        continue;                                                                                                  //不为window C#类型的项目跳过
                    }
                    var projectValueArr = projectContextArr[1].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); //取出项目属性文本信息
                    if (projectValueArr.Length != 3)
                    {
                        continue;
                    }

                    projInfo = new SolutionProjectInfo()
                    {
                        ProjTypeGuid = projectTypeGuid,
                        ProjName     = new Regex(AppConst.Exp_ProjName, RegexOptions.Compiled | RegexOptions.IgnoreCase).Match(projectValueArr[0].Trim()).Value,
                        ProjFilePath = new Regex(AppConst.Exp_ProjFilePath, RegexOptions.Compiled | RegexOptions.IgnoreCase).Match(projectValueArr[1].Trim()).Value,
                        ProjGuid     = new Regex(AppConst.Exp_ProjGuid, RegexOptions.Compiled | RegexOptions.IgnoreCase).Match(projectValueArr[2].Trim()).Value
                    };
                    projInfo.ProjDirPath = Path.GetDirectoryName(dirPath.TrimEnd(new char[] { '\\', '\\' }) + "\\" + projInfo.ProjFilePath.TrimStart(new char[] { '\\', '\\' }));
                    projList.Add(projInfo);
                }
                return(new SolutionInfo()
                {
                    DirPath = dirPath,
                    FullFilePath = slnFilePath,
                    ProjectInfos = projList
                });
            }
            catch (Exception ex)
            {
                throw new Exception("读取解决方案项目工程信息发生异常.", ex);
            }
        }
Example #2
0
 public ProjectListItem(SolutionProjectInfo projectInfo, bool isSelected)
 {
     ProjectInfo = projectInfo;
     IsSelected  = isSelected;
 }