내 프로젝트, 도구 코딩 프로젝트를 열 감지하는 데 필요한 모든 항목을 얻을. 하지만 폴더, 프로젝트의 폴더가있는 경우 내가 모든 항목 SLN 파일을 얻을 수있는 방법을 찾았다 있도록 방법은, 다음 프로젝트를 사용할 수없는 것으로 나타났습니다.
이 방법은 이전에 사용 dte.Solution.Projects
하지만, 프로젝트 폴더가 얻을에, 그래서 제공하는 스택을 사용합니다.
첫째, 참조 추가 Microsoft.Build
노트 버전
그럼 난 프로젝트에 세 개의 클래스가, 사실이 잘 개 세부 사항을 넣어 내 참조 github의를
public class Solution
{
//internal class SolutionParser
//Name: Microsoft.Build.Construction.SolutionParser
//Assembly: Microsoft.Build, Version=4.0.0.0
static readonly Type s_SolutionParser;
static readonly PropertyInfo s_SolutionParser_solutionReader;
static readonly MethodInfo s_SolutionParser_parseSolution;
static readonly PropertyInfo s_SolutionParser_projects;
static Solution()
{
//s_SolutionParser_projects.GetValue()
s_SolutionParser = Type.GetType("Microsoft.Build.Construction.SolutionParser, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
if (s_SolutionParser != null)
{
s_SolutionParser_solutionReader = s_SolutionParser.GetProperty("SolutionReader", BindingFlags.NonPublic | BindingFlags.Instance);
s_SolutionParser_projects = s_SolutionParser.GetProperty("Projects", BindingFlags.NonPublic | BindingFlags.Instance);
s_SolutionParser_parseSolution = s_SolutionParser.GetMethod("ParseSolution", BindingFlags.NonPublic | BindingFlags.Instance);
}
}
public List<SolutionProject> Projects { get; private set; }
public List<SolutionConfiguration> Configurations { get; private set; }
public Solution(string solutionFileName)
{
if (s_SolutionParser == null)
{
throw new InvalidOperationException("Can not find type 'Microsoft.Build.Construction.SolutionParser' are you missing a assembly reference to 'Microsoft.Build.dll'?");
}
var solutionParser = s_SolutionParser.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First().Invoke(null);
using (var streamReader = new StreamReader(solutionFileName))
{
s_SolutionParser_solutionReader.SetValue(solutionParser, streamReader, null);
s_SolutionParser_parseSolution.Invoke(solutionParser, null);
}
var projects = new List<SolutionProject>();
var array = (Array)s_SolutionParser_projects.GetValue(solutionParser, null);
for (int i = 0; i < array.Length; i++)
{
projects.Add(new SolutionProject(array.GetValue(i)));
}
this.Projects = projects;
GetProjectFullName(solutionFileName);
//Object cfgArray = //s_SolutionParser_configurations.GetValue
// s_SolutionParser_projects.GetValue(solutionParser, null);
//PropertyInfo[] pInfos = null;
//pInfos = cfgArray.GetType().GetProperties();
//int count = (int)pInfos[1].GetValue(cfgArray, null);
//var configs = new List<SolutionConfiguration>();
//for (int i = 0; i < count; i++)
//{
// configs.Add(new SolutionConfiguration(pInfos[2].GetValue(cfgArray, new object[] { i })));
//}
//this.Configurations = configs;
}
private void GetProjectFullName(string solutionFileName)
{
DirectoryInfo solution = (new FileInfo(solutionFileName)).Directory;
foreach (var temp in Projects.Where
//(temp=>temp.RelativePath.EndsWith("csproj"))
(temp => !temp.RelativePath.Equals(temp.ProjectName))
)
{
GetProjectFullName(solution, temp);
}
}
private void GetProjectFullName(DirectoryInfo solution, SolutionProject project)
{
//Uri newUri =new Uri(,UriKind./*Absolute*/);
//if(project.RelativePath)
project.FullName = System.IO.Path.Combine(solution.FullName, project.RelativePath);
}
}
[DebuggerDisplay("{ProjectName}, {RelativePath}, {ProjectGuid}")]
public class SolutionProject
{
static readonly Type s_ProjectInSolution;
static readonly PropertyInfo s_ProjectInSolution_ProjectName;
static readonly PropertyInfo s_ProjectInSolution_RelativePath;
static readonly PropertyInfo s_ProjectInSolution_ProjectGuid;
static readonly PropertyInfo s_ProjectInSolution_ProjectType;
static SolutionProject()
{
s_ProjectInSolution = Type.GetType("Microsoft.Build.Construction.ProjectInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
if (s_ProjectInSolution != null)
{
s_ProjectInSolution_ProjectName = s_ProjectInSolution.GetProperty("ProjectName", BindingFlags.NonPublic | BindingFlags.Instance);
s_ProjectInSolution_RelativePath = s_ProjectInSolution.GetProperty("RelativePath", BindingFlags.NonPublic | BindingFlags.Instance);
s_ProjectInSolution_ProjectGuid = s_ProjectInSolution.GetProperty("ProjectGuid", BindingFlags.NonPublic | BindingFlags.Instance);
s_ProjectInSolution_ProjectType = s_ProjectInSolution.GetProperty("ProjectType", BindingFlags.NonPublic | BindingFlags.Instance);
}
}
public string ProjectName { get; private set; }
public string RelativePath { get; private set; }
public string ProjectGuid { get; private set; }
public string ProjectType { get; private set; }
public string FullName { set; get; }
public SolutionProject(object solutionProject)
{
this.ProjectName = s_ProjectInSolution_ProjectName.GetValue(solutionProject, null) as string;
this.RelativePath = s_ProjectInSolution_RelativePath.GetValue(solutionProject, null) as string;
this.ProjectGuid = s_ProjectInSolution_ProjectGuid.GetValue(solutionProject, null) as string;
this.ProjectType = s_ProjectInSolution_ProjectType.GetValue(solutionProject, null).ToString();
}
}
public class SolutionConfiguration
{
static readonly Type s_ConfigInSolution;
static readonly PropertyInfo configInSolution_configurationname;
static readonly PropertyInfo configInSolution_fullName;
static readonly PropertyInfo configInSolution_platformName;
static SolutionConfiguration()
{
s_ConfigInSolution = Type.GetType("Microsoft.Build.Construction.ConfigurationInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
if (s_ConfigInSolution != null)
{
configInSolution_configurationname = s_ConfigInSolution.GetProperty("ConfigurationName", BindingFlags.NonPublic | BindingFlags.Instance);
configInSolution_fullName = s_ConfigInSolution.GetProperty("FullName", BindingFlags.NonPublic | BindingFlags.Instance);
configInSolution_platformName = s_ConfigInSolution.GetProperty("PlatformName", BindingFlags.NonPublic | BindingFlags.Instance);
}
}
public string configurationName { get; private set; }
public string fullName { get; private set; }
public string platformName { get; private set; }
public SolutionConfiguration(object solutionConfiguration)
{
this.configurationName = configInSolution_configurationname.GetValue(solutionConfiguration, null) as string;
this.fullName = configInSolution_fullName.GetValue(solutionConfiguration, null) as string;
this.platformName = configInSolution_platformName.GetValue(solutionConfiguration, null) as string;
}
}
참고로 인용
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
사용되는 코드 위의 작은 주로 반영 말한다.
반사와 해결받을 SLN s_SolutionParser_parseSolution
그는 모든 항목을 얻을 수 있습니다.
그러나 프로젝트 얻은 경로가 상대적 후 사용 상대 경로 절대 경로를 설정 C # .NET을 경로는 변환 된 프로젝트가 될 수 있습니다.
용도
프로젝트 이름이 자동으로 모든 항목을 얻을 것이다 입력과 같은 프로젝트 파일 이름을 입력합니다.
Solution solution = new Solution(工程文件路径);
모든 프로젝트에 대한 프로젝트 파일을 가져 오기
foreach (var temp in solution.Projects)
{
}
코드 : https://gist.github.com/lindexi/b36feb816fe9e586ffbbdf58397b25da
http://stackoverflow.com/questions/707107/parsing-visual-studio-solution-files
이 문서는 자주 업데이트됩니다, 원래 텍스트 읽어 보시기 바랍니다 https://blog.lindexi.com/post/C-%E8%A7%A3%E6%9E%90-sln-%E6%96%87%E4%BB%B6을 .html 중에서 순서대로 지식의 이전 오류 및 더 나은 독서 경험을 오해의 소지가 방지 할 수 있습니다.
당신이 내 최신 블로그를 읽고 계속하려면를 클릭하십시오 RSS 피드 , 사용하는 것이 좋습니다 RSS 스토커를 블로그, 또는 구독 내 홈페이지 CSDN 우려를 방문
이 작품은 비영리 - - 동일 조건 변경 허락 4.0 국제 라이센스 계약 크리에이티브 커먼즈 저작자 표시 라이선스합니다. 무단 전재, 사용, 재 게시에 오신 것을 환영합니다,하지만 (링크가 포함 : 린데 사이가 서명 한 문서를 보관하십시오 https://blog.lindexi.com를 ) 상업적인 목적으로 사용할 수 없다, 용지 사용권 변경에 따라 같은 일을 게시해야합니다. 당신은 질문이있는 경우, 제발 저에게 연락 .
다음은 시간을 광고하고
권장 공공 우려 Edi.Wang 번호