title author date CreateTime categories
C# 解析 sln 文件
lindexi
2018-2-13 17:23:3 +0800
2018-2-13 17:23:3 +0800
C#

我的项目,编码工具 需要检测打开一个工程,获取所有项目。
但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得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# 相对路径转绝对路径,可以转换项目路径。

使用

输入工程文件名就好,输入工程名,会自动获得所有项目。

  Solution solution = new Solution(工程文件路径);

获得工程文件的所有项目

  foreach (var temp in solution.Projects)
{ }

<script src="https://gist.github.com/lindexi/b36feb816fe9e586ffbbdf58397b25da.js"></script>

代码:https://gist.github.com/lindexi/b36feb816fe9e586ffbbdf58397b25da

参见:https://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project.propertygroups%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

https://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.project%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

http://stackoverflow.com/questions/707107/parsing-visual-studio-solution-files

2018-2-13-C#-解析-sln-文件的更多相关文章

  1. C# 解析 sln 文件

    我的项目,编码工具 需要检测打开一个工程,获取所有项目. 但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得sln文件的所有项目. 原先使用的方法d ...

  2. CSharpGL(9)解析OBJ文件并用CSharpGL渲染

    CSharpGL(9)解析OBJ文件并用CSharpGL渲染 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo ...

  3. Android开发学习---使用XmlPullParser解析xml文件

    Android中解析XML的方式主要有三种:sax,dom和pull关于其内容可参考:http://blog.csdn.net/liuhe688/article/details/6415593 本文将 ...

  4. OAF_文件系列5_实现OAF解析XML文件javax.xml.parsers(案例)

    20150729 Created By BaoXinjian

  5. 解析XML文件的几种常见操作方法—DOM/SAX/DOM4j

    解析XML文件的几种常见操作方法—DOM/SAX/DOM4j 一直想学点什么东西,有些浮躁,努力使自己静下心来看点东西,哪怕是回顾一下知识.看到了xml解析,目前我还没用到过.但多了解一下,加深点记忆 ...

  6. Java环境解析apk文件信息

    概述:Java解析apk文件,获取apk文件里的包名,版本号,图标文件等; 功能:可以提供给windows和linux平台使用; 原理:利用aapt.exe或者aapt这些anroid平台解析apk文 ...

  7. 解析 MACH_O 文件

    现在做iOS开发的挺多,了解一下在苹果平台上程序运行的原理 解析 MACH_O 文件 这篇文章描述了如何解析 Mach-O 文件并稍微解释了一下它的格式.这不是一份权威指南,不过当你不知从何开始时,它 ...

  8. java解析properties文件

    在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理proper ...

  9. 解析XML文件示例

    项目中要解析Xml文件,于是在工程里找了下前人写例子. 1,SAX(基于事件,效率高,使用声明加载什么). public class MVCConfig { private static MVCCon ...

  10. [android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found

    google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展.可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了.之前导4.0 ...

随机推荐

  1. Deserializing/Serializing SOAP Messages in C#

    /// <summary>   /// Converts a SOAP string to an object   /// </summary>   /// <typep ...

  2. JavaScript--DOM操作例子:隔行变色

    上效果: 实现思想: 主要是js动态创建标签,还有动态结合css实现样式 <!DOCTYPE html> <html lang="en"> <head ...

  3. Git同步Python代码

    之前我们都是将代码保存到本地目录, 然后再上传到Git中,但如果针对在pycharm中的代码,就要换另一种方式了,下面简单介绍一下. 1.打开pycharm主界面,选择菜单栏VCS---checkou ...

  4. 【JZOJ4878】【NOIP2016提高A组集训第10场11.8】时空传送

    题目描述 经过旷久的战争,ZMiG和707逐渐培养出了深厚的感♂情.他们逃到了另一块大陆上,决定远离世间的纷争,幸福地生活在一起.钟情707的neither_nor决心要把他们拆散,他要动用手中最大杀 ...

  5. 【JZOJ4840】【NOIP2016提高A组集训第4场11.1】小W砍大树

    题目描述 数据范围 解法 模拟. 代码 #include<stdio.h> #include<algorithm> #include<string.h> #incl ...

  6. 在Debug模式下,如何给.lib和.dll添加一个d标记(*d.lib,*d.dll)

    选中工程->右键->属性->配置属性->常规,可以看到项目默认值的配置类型有好几种类型,选择静态库类型生成lib文件,选择动态库类型生成dll文件,选择应用程序生成exe文件, ...

  7. ural1297 后缀数组+RMQ

    RMQ即求区间(i,j)的最值.通过O(nlogn)处理,O(1)给出答案. RMQ主要是动态规划来做.dp[i][j]表示从i开始的长为2^j的区间最值. 那么可以得到dp[i][j]=max(dp ...

  8. oracle表复杂查询--创建数据库实例

    n  创建数据库有两种方法: 1)通过oracle提供的向导工具 2)我们可以用手工步骤直接创建 但我们创建完一个新的数据库实例后,在服务中就会有两个新的服务创建,这时,你根据实际需要去启动相应的数据 ...

  9. 《mysql必知必会》笔记2(子查询、联接、组合查询、全文本搜索)

    十四:使用子查询 1:子查询是嵌套在其他查询中的查询. 2:需要列出订购TNT2的所有客户信息,需要下面几步: a:从orderitems表中检索出包含物品TNT2的所有订单号: b:根据上一步得出的 ...

  10. Libevent:1前言

    一:libevent概述: libevent是一个用来编写快速.可移植.非阻塞IO程序的库,它的设计目标是:可移植性.高效.可扩展性.便捷. libevent包含下列组件: evutil:对不同平台下 ...