How do I programmatically list all projects in a solution?

前端 未结 12 1460
感动是毒
感动是毒 2020-11-29 04:06

How do I programmatically list all of the projects in a solution? I\'ll take a script, command-line, or API calls.

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 04:30

        var Content = File.ReadAllText(SlnPath);
        Regex projReg = new Regex(
            "Project\\(\"\\{[\\w-]*\\}\"\\) = \"([\\w _]*.*)\", \"(.*\\.(cs|vcx|vb)proj)\""
            , RegexOptions.Compiled);
        var matches = projReg.Matches(Content).Cast();
        var Projects = matches.Select(x => x.Groups[2].Value).ToList();
        for (int i = 0; i < Projects.Count; ++i)
        {
            if (!Path.IsPathRooted(Projects[i]))
                Projects[i] = Path.Combine(Path.GetDirectoryName(SlnPath),
                    Projects[i]);
            Projects[i] = Path.GetFullPath(Projects[i]);
        }
    

    Edit: Amended the regex to include the ".*" as per the comment by Kumar Vaibhav

提交回复
热议问题