Visual Studio创建项目时.sln文件发生了什么?

@adens 2/28/2022 8:23:01 AM

Visual Studio创建项目时.sln文件发生了什么?

通过使用dotnet命令来查看新建项目的时候visual studio 的 .sln文件有哪些改变的.以及.sln文件时如何组织项目的.

  1. dotnet new sln 创建的空项目

sln 里面的内容

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal
  1. dotnet sln add 添加一个 项目

首先需要dotnet new 新建一个项目

dotnet new mvc -n test.ui

创建一个mvc项目

执行 dotnet sln add

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test.ui", "test.ui\test.ui.csproj", "{43C4BC58-2140-46FD-AE52-3FC4204E4712}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal

对比之下Global之前多了

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test.ui", "test.ui\test.ui.csproj", "{43C4BC58-2140-46FD-AE52-3FC4204E4712}" EndProject

GlobalSection(ProjectConfigurationPlatforms) = postSolution 
{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 
{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Debug|Any CPU.Build.0 = Debug|Any CPU 
{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Release|Any CPU.ActiveCfg = Release|Any CPU 
{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Release|Any CPU.Build.0 = Release|Any CPU 
EndGlobalSection
  1. 使用dotnet new classlib -n mylib1 创建一个lib

dotnet sln add -s src mylib1 添加mylib1 到src项目目录下

对比 2

Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{07763AA8-7D9F-475A-9DF5-0B14AC032100}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mylib1", "mylib1\mylib1.csproj", "{F663A978-6826-4CE8-9221-D2F888946856}"
EndProject
{F663A978-6826-4CE8-9221-D2F888946856}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F663A978-6826-4CE8-9221-D2F888946856}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F663A978-6826-4CE8-9221-D2F888946856}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F663A978-6826-4CE8-9221-D2F888946856}.Release|Any CPU.Build.0 = Release|Any CPU
GlobalSection(NestedProjects) = preSolution
{F663A978-6826-4CE8-9221-D2F888946856} = {07763AA8-7D9F-475A-9DF5-0B14AC032100}
EndGlobalSection

下面开始总结.

sln 项目组成

每次在sln中添加一个项目会进行以下步骤

  1. 如果添加在根目录

添加一个 Project

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test.ui", "test.ui\test.ui.csproj", "{43C4BC58-2140-46FD-AE52-3FC4204E4712}"
EndProject

在GlobalSection(ProjectConfigurationPlatforms) = postSolution里面添加

{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Release|Any CPU.ActiveCfg = Release|Any CPU
{43C4BC58-2140-46FD-AE52-3FC4204E4712}.Release|Any CPU.Build.0 = Release|Any CPU
  1. 如果添加在一个项目目录里面 就是 -s src 会创建一个 src 的Project
    Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{07763AA8-7D9F-475A-9DF5-0B14AC032100}"
    EndProject
    

在创建 1. 的Project和GlobalSection(ProjectConfigurationPlatforms) = postSolution

在GlobalSection(NestedProjects) = preSolution里添加

{F663A978-6826-4CE8-9221-D2F888946856} = {07763AA8-7D9F-475A-9DF5-0B14AC032100}

表示将project 放在src目录下

sln Project 字段解释

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test.ui", "test.ui\test.ui.csproj", "{43C4BC58-2140-46FD-AE52-3FC4204E4712}"
EndProject

抽象一下

Project("{ProjectTypeGuids}") = "ProjectName","ProjectPath","ProjectGuid"
EndProject

ProjectTypeGuid有明确的分类,这个是查看dotnet/sdk源代码发现的

namespace Microsoft.DotNet.Cli.Sln.Internal
{
    public static class ProjectTypeGuids
    {
        public const string CSharpProjectTypeGuid = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
        public const string FSharpProjectTypeGuid = "{F2A71F9B-5D33-465A-A702-920D77279786}";
        public const string VBProjectTypeGuid = "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}";
        public const string SolutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}";
    }
}

ProjectGuid则是随机生成的,代表这个Project,在之后的步骤中都是用的这个Guid

例如在GlobalSection(ProjectConfigurationPlatforms) = postSolution

里添加的平台配置

{ProjectGuid}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ProjectGuid}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ProjectGuid}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ProjectGuid}.Release|Any CPU.Build.0 = Release|Any CPU

又或者在配置项目所在目录里GlobalSection(NestedProjects) = preSolution

{ProjectGuid} = {FolderGuid}

FolderGuidsrcProjectGuid两个都是随机生成的

Last Modification : 2/28/2022 8:23:01 AM


In This Document