问题1: c# ProjectText 的默认解决方案文件的格式是否始终相同?
Project(Solution GUI) = SolutionName , ProjectPath, csProjPath, ProjectGUI
e.g:
Project("{FAE14EC0-321D-12D3-GF35-01C04F79EFBC}") = "WindowsFormsApplication26", "WindowsFormsApplication26\WindowsFormsApplication26.csproj", "{25F0453B-9C88-4C9E-AG6A-97873BB6EA23}"
EndProject
格式如下:1,它将具有解决方案GUI,然后是solutionName,然后是csproj 路径然后projectGUI
问题2: 我已经创建了一个正则表达式来获取.sln生成的文本,这就是我所拥有的:
Regex projectRegex = new Regex(@"Project\(\""([^\""]+)\""\)\s*\=\s*\""([^\""]+)\""\,\s*\""([^\""]+)""\,\s*\""([^\""]+)""");
但我不确定我的正则表达式中是否存在与第一个问题相关的错误,所以我想如果有更好的正则表达式或任何条件(例如解析或任何必须更快)才能获得ProjectText < / em>具体(我需要得到的)是csProj路径(在Group3上面的正则表达式模式上)
以及在Q1上显示的示例中应用的组:
Group1: {FAE14EC0-321D-12D3-GF35-01C04F79EFBC}
Group2: WindowsFormsApplication26
Group3: WindowsFormsApplication26\WindowsFormsApplication26.csproj
Group4: {25F0453B-9C88-4C9E-AG6A-97873BB6EA23}
答案 0 :(得分:0)
我认为你的方法应该正确工作但是为了从解决方案文件中提取信息,我们通过访问“Microsoft.Build”的内部技术使用以下方法。命名空间。由于相应的框架功能被标记为internal
,因此有必要public struct ProjectData
{
public string Name;
public string Guid;
public string Path;
}
public static List<ProjectData> GetProjectsInSolution(string pathToSolutionFile)
{
Type parserType = Type.GetType("Microsoft.Build.Construction.SolutionParser, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
ConstructorInfo constructorInfo = parserType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First();
object solutionParser = constructorInfo.Invoke(null);
PropertyInfo solutionFileProperty = parserType.GetProperty("SolutionFile", BindingFlags.NonPublic | BindingFlags.Instance);
solutionFileProperty.SetValue(solutionParser, pathToSolutionFile);
MethodInfo parserMethod = parserType.GetMethod("ParseSolutionFile", BindingFlags.NonPublic | BindingFlags.Instance);
parserMethod.Invoke(solutionParser, null);
PropertyInfo projectInfo = parserType.GetProperty("Projects", BindingFlags.NonPublic | BindingFlags.Instance);
object[] projects = projectInfo.GetValue(solutionParser) as object[];
Type projectType = Type.GetType("Microsoft.Build.Construction.ProjectInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
PropertyInfo nameInfo = projectType.GetProperty("ProjectName", BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo guidInfo = projectType.GetProperty("ProjectGuid", BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo pathInfo = projectType.GetProperty("RelativePath", BindingFlags.NonPublic | BindingFlags.Instance);
List<ProjectData> projectData = new List<ProjectData>();
if (projects != null)
{
foreach (object project in projects)
{
projectData.Add(new ProjectData()
{
Name = nameInfo.GetValue(project) as string,
Guid = guidInfo.GetValue(project) as string,
Path = pathInfo.GetValue(project) as string
});
}
}
return projectData;
}