C# 解析 sln 文件

article/2025/10/8 4:54:19

我的项目,编码工具 需要检测打开一个工程,获取所有项目。

但是发现原来的方法,如果存在文件夹,把项目放在文件夹中,那么是无法获得项目,于是我就找了一个方法去获得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.0static 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){}

代码: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

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。


http://chatgpt.dhexx.cn/article/weyGJ4yi.shtml

相关文章

Visual studio无法打开C#\.sln文件,不兼容

23/7/19文章更新&#xff1a;后来我总结了我这个问题出现的原因&#xff0c;是因为我的VS是2015版本&#xff0c;本来安装在笔记本电脑上&#xff0c;为了转到台式机&#xff0c;直接U盘复制过来的&#xff0c;然后复制过来安装的时候提示某个东西安装失败&#xff0c;我就点击…

.sln图标异常修复

.sln图标显示异常 本人就喜欢新版本的东西&#xff0c;电脑上安装过vs2017,vs2019&#xff0c;想体验最新的vs2022是什么感觉&#xff0c;之后sln图标显示异常&#xff0c;不太顺眼 这是由于该文件VSFileHandler_64.dll异常带来的问题&#xff0c;下载我提供的文件&#xff0c…

Visual Studio打开无sln项目,修复无效sln文件

Visual Studio打开无sln项目&#xff0c;修复无效sln文件 打开项目文件地址&#xff08;可在项目名右键-在文件资源管理器中打开文件夹&#xff09; 打开-项目名.vcxproj文件 VC左上角点击全部保存&#xff0c;选择保存位置储存新的sln头文件 4.sln文件修复完成

VS2019 使用命令行编译工程sln

需要使用 devenv.com这个工具 具体的执行如下&#xff1a; test>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.com" build\1.sln /Build执行结果如下&#xff1a; 在对应的路径下找到可执行文件运行

Visual studio 2019 创建.sln项目

文章目录 创建.sln项目在当前解决方案下&#xff0c;添加项目如何切换启动项目如何打开 .sln的解决方案的项目 创建.sln项目 文件–>新建 不要勾选最后一项。 点击 上面界面的右下角的 创建 &#xff0c;进入下面的界面 解决方案和项目在电脑目录及在IDE上的展示 在当…

Unity sln 和 csproj 基础

根目录下 sln 和 csproj 区别 sln&#xff1a; solusion 简写&#xff0c;即解决方案 csproj&#xff1a;c sharp project 简写&#xff0c;即 C# 项目 解决方案sln是项目csproj的集合&#xff0c;项目是文件的集合。 一个 sln 中可以包含多个 csproj。 一个 csproj 可以包含多…

c语言编程题没有sln,使用CMake生成sln项目和VS工程遇到的问题

用vs运行cmake后的工程 1、单个文件示例: 1) 首先建立文件夹CMakeTest/Src 2) 在文件夹Src中建立两个文件main.c和CMakeLists.txt 3) main.c: #include int main() {printf("hello world."); getchar(); return 0; } 4) CMakeLists.txt PROJECT (HELLO) SET (SRC_L…

理解 Visual Studio 解决方案文件格式(.sln)

一般情况下我们并不需要关心 Visual Studio 解决方案文件格式&#xff08;.sln&#xff09;&#xff0c;因为 Visual Studio 对解决方案文件的自动修复能力是非常强的。但是如果遇到自动解冲突错误或者编译不通过了&#xff0c;那么此文件还是需要手工修改的。 基本概念 Visua…

Win平台使用cmake工具生成sln工程示例

先安装一个版本的cmake&#xff0c;3.17.2; 这应该是比较新的版本&#xff1b;我看到有的示例是3.7以下版本&#xff1b; cmake加到系统path变量&#xff1b; 安装完成&#xff1b; 新建一个C#prj目录&#xff0c;下面放一个cs文件&#xff0c;新建一个myprj1目录&#xff1b; …

linux系统sln命令,dotnet sln

dotnet slndotnet sln 12/07/2020 本文内容 本文适用于&#xff1a; ✔️ .NET Core 2.x SDK 及更高版本This article applies to: ✔️ .NET Core 2.x SDK and later versions “属性”Name dotnet sln - 在 .NET 解决方案文件中列出或修改项目。dotnet sln - Lists or modifi…

【通用】vs2019项目sln、suo、vcxproj、vcxproj.filters、vcxproj.user文件名解析及相关节点属性解析

1.sln 文件 (Solution 文件) 解决方案是在 Visual Studio 中组织项目的结构。 该解决方案将项目的状态信息保留在两个文件中&#xff1a; .sln 文件 (基于文本的共享) .suo 文件 (用户特定的二进制解决方案选项) 这里先讲.sln 文件&#xff0c;.sln 文件包含环境用于查找和加载…

cosi-corr操作详细步骤

cosi-corr是一个可以加载到ENVI的软件包&#xff0c;由加利福尼亚理工学院的Franois Ayoub, Sbastien Leprince, and Lionel Keene开发&#xff0c;并且提供源代码&#xff0c;该软件受到了NSF的资助。软件的主要功能是可以满足各种影像的正射校正和影像配准。 针对下载下来的…

pearsonr(x,y)、corr()、corrcoef(u1) 相关系数计算

函数&#xff1a;pearsonr(x,y) 功能&#xff1a; 计算特征与目标变量之间的相关度 参数说明&#xff1a; 1&#xff09;输入&#xff1a;x为特征&#xff0c;y为目标变量. 2&#xff09;输出&#xff1a;r&#xff1a; 相关系数 [-1&#xff0c;1]之间&#xff0c;p-value: …

pandas.DataFrame.corr求解变量列相关系数与可视化展示

pandas.DataFrame.corr求解变量列相关系数与可视化展示 目录 pandas.DataFrame.corr求解变量列相关系数与可视化展示 1常见的三种相关系数2 pandas.DataFrame.corr用法3 结果可视化 1常见的三种相关系数 Pearson相关系数&#xff1a;度量两变量之间的线性相关性&#xff1b;对…

【20220623】【信号处理】深入理解Pearson相关系数和Matlab corr()、corrcoef()仿真

目录 一、定义 二、特性 三、适用条件 四、Matlab 仿真 1. 时间序列 2. 矩阵 一、定义 相关系数&#xff08;correlation of coefficient&#xff09;是统计学中的概念&#xff0c;是由统计学家卡尔皮尔逊设计的一个统计指标&#xff0c;也称作 Pearson 相关系数。相关系…

python计算两组数据的相关性_关于python:使用.corr获取两列之间的相关性

我有以下熊猫数据框Top15&#xff1a; 我创建了一个列来估计每个人的可引用文档数&#xff1a; 1 2Top15[PopEst] Top15[Energy Supply] / Top15[Energy Supply per Capita] Top15[Citable docs per Capita] Top15[Citable documents] / Top15[PopEst] 我想知道人均可引用…

df.corr和df.describe()

1&#xff1a;df.corr&#xff08;&#xff09; df.corr()函数的作用是返回列与列之间的相关系数 corr_matrix df.corr() sns.heatmap(corr_matrix, annotTrue, cmapcoolwarm) 2:df.describe() 得到数字列的一些特殊值。如果是df.describe(includeO) &#xff0c;则描述obje…

Oracle MySQL Hive sql 求相关性系数 corr

MySQL所有版本&#xff1a; CREATE TABLE sample (x float NOT NULL,y float NOT NULL,user_name varchar(255) ) ;INSERT INTO sample VALUES (1, 10, zs); INSERT INTO sample VALUES (2, 4, zs); INSERT INTO sample VALUES (3, 5, zs); INSERT INTO sample VALUES (6, 17…

CORR函数 看不明白

CORR聚集函数来计算相关系数 CORR&#xff1a;皮尔逊相关系数&#xff0c;是用于度量两个变量X和Y之间的相关&#xff08;线性相关&#xff09;&#xff0c;其值介于-1与1之间。 CORR_S&#xff1a;斯皮尔曼等级相关 SELECT CORR(SYSDATE - hiredate, sal) AS corr_val, CORR…

COSI-Corr安装教程

hello,各位好久不见。最近要处理数据了&#xff0c;一步步学习COSI-Corr软件。安装很简单的&#xff0c;大家一起来学习吧。 1、在COSI-Corr官网点击并注册&#xff0c;官方会给你提供的邮箱发送软件安装包&#xff1b;COSI-Corr: Measuring Ground Deformation from Optical …