一直使用Visual Studio开发项目,今天特意使用记事本打开基本的.sln和.csproj文件。
使用Visual Studio新建项目,会自动产生.sln和.csproj文件
.sln文件:
Solution 解决方案文件,记录编译,以及使用那些类库,项目.本质是一个VB文件
.csproj文件:
C?Sharp Project C#项目文件,记录文件的行为,是否编译,引用那些类库和服务引用等。
表示一个类库dll或者可执行程序exe,表示有哪些类文件,资源文件等.
本质是xml文件
以一个简单的读取json文件为例
1.新建控制台应用程序TestFileDefineDemoVS,在项目TestFileDefineDemoVS下新建文件夹dll,在文件夹dll下添加文件Newtonsoft.Json.dll。
2.选中解决方案TestFileDefineDemoVS,新建项目-类库,命名为ClassLibraryTest,在类库项目ClassLibraryTest下新建类JsonFileUtil。添加对Newtonsoft.Json.dll类库的引用。
类JsonFileUtil源代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
namespace ClassLibraryTest
{
public class JsonFileUtil
{
public static string JsonConfigFileName = "xx.Json";
#region 配置文件读写
/// <summary>
/// 将配置对象写入程序执行目录指定名称的文件
/// </summary>
/// <param name="config">配置对象</param>
/// <param name="strConfigFileFullPath">配置文件绝对路径</param>
/// <returns></returns>
public static string SaveConfig(object config, string strConfigFileFullPath, Encoding encoding = null)
{
string strRes = string.Empty;
try
{
if (config == null)
{
if (File.Exists(strConfigFileFullPath))
{
File.Delete(strConfigFileFullPath);
}
}
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(config, Formatting.Indented);
if (encoding == null)
{
encoding = Encoding.Default;
}
File.WriteAllText(strConfigFileFullPath, strJson, encoding);
}
catch (Exception ex)
{
strRes = ex.Message;
}
return strRes;
}
/// <summary>
/// 将配置对象写入程序执行目录指定名称的文件
/// </summary>
/// <param name="config">配置对象</param>
/// <param name="strStartPath">文件存放目录</param>
/// <param name="strConfigFileName">配置文件名称</param>
/// <returns></returns>
public static string SaveConfig(object config, string strStartPath, string strConfigFileName = null, Encoding encoding = null)
{
string strRes = string.Empty;
string strFileName = string.IsNullOrEmpty(strConfigFileName) ? JsonConfigFileName : strConfigFileName;
string strPath = Path.Combine(strStartPath, strFileName);
return SaveConfig(config, strPath, encoding);
}
/// <summary>
/// 读取配置文件的配置信息对象
/// </summary>
/// <typeparam name="T">泛型类型</typeparam>
/// <param name="strConfigFileFullPath">配置文件的绝对路径</param>
/// <returns></returns>
public static T ReadConfig<T>(string strConfigFileFullPath) where T : class
{
T oRes = default(T);
try
{
if (File.Exists(strConfigFileFullPath))
{
string strJson = File.ReadAllText(strConfigFileFullPath, Encoding.Default);
if (!string.IsNullOrEmpty(strJson))
{
object oConvertRes = Newtonsoft.Json.JsonConvert.DeserializeObject(strJson, typeof(T));
if (oConvertRes != null)
{
oRes = oConvertRes as T;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return oRes;
}
/// <summary>
/// 读取配置对象信息
/// </summary>
/// <typeparam name="T">配置对象泛型类型信息</typeparam>
/// <param name="strStartPath">程序启动路径</param>
/// <param name="strName">配置文件名称</param>
/// <returns></returns>
public static T ReadConfig<T>(string strStartPath, string strName) where T : class
{
string strPath = Path.Combine(strStartPath, strName);
return ReadConfig<T>(strPath);
}
#endregion
}
}
3.选择控制台应用程序TestFileDefineDemoVS,添加对类库ClassLibraryTest的引用。新建实体类Employee,如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestFileDefineDemoVS
{
public class Employee
{
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
public override string ToString()
{
return $"{{姓名:{Name},地址:{Address},年龄:{Age}}}";
}
}
}
4.选择控制台应用程序TestFileDefineDemoVS,新建测试类JsonHelper,源程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibraryTest;
namespace TestFileDefineDemoVS
{
public class JsonHelper
{
public static void TestJson(string jsonFile)
{
Employee employee = JsonFileUtil.ReadConfig<Employee>(jsonFile);
Console.WriteLine(employee);
}
}
}
新建json文件employee.json,设置为始终复制,employee.json文件内容
{
"Name": "月清疏",
"Address": "明庶门",
"Age": 18
}
在默认的Program输入测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestFileDefineDemoVS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello");
JsonHelper.TestJson(AppDomain.CurrentDomain.BaseDirectory + "employee.json");
Console.ReadLine();
}
}
}
程序运行如图:
?解决方案文件TestFileDefineDemoVS.sln,使用记事本打开
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.1831
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestFileDefineDemoVS", "TestFileDefineDemoVS\TestFileDefineDemoVS.csproj", "{4F66C7E6-56BC-4FB9-A9A0-3720089299E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClassLibraryTest", "ClassLibraryTest\ClassLibraryTest.csproj", "{CF20E151-BAE2-44D0-BDDB-F67421F049EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4F66C7E6-56BC-4FB9-A9A0-3720089299E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F66C7E6-56BC-4FB9-A9A0-3720089299E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F66C7E6-56BC-4FB9-A9A0-3720089299E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F66C7E6-56BC-4FB9-A9A0-3720089299E4}.Release|Any CPU.Build.0 = Release|Any CPU
{CF20E151-BAE2-44D0-BDDB-F67421F049EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF20E151-BAE2-44D0-BDDB-F67421F049EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF20E151-BAE2-44D0-BDDB-F67421F049EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF20E151-BAE2-44D0-BDDB-F67421F049EE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {05672AEA-55CD-4D6D-9DF2-54BE09432F7D}
EndGlobalSection
EndGlobal
关键节点说明: 前五行表示VS版本为VS2017,当前版本和最低兼容版本。
Project---EndProject 节点 表示 该解决方案有哪些项目,
如本解决方案由两个项目组成 TestFileDefineDemoVS\TestFileDefineDemoVS.csproj,
ClassLibraryTest\ClassLibraryTest.csproj
Global---EndGlobal节点表示Debug和Release等配置
C#项目文件ClassLibraryTest.csproj,使用记事本打开,我们发现本质是个xml文件
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CF20E151-BAE2-44D0-BDDB-F67421F049EE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ClassLibraryTest</RootNamespace>
<AssemblyName>ClassLibraryTest</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\TestFileDefineDemoVS\dll\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="JsonFileUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
主要节点说明
Import:一般按默认处理
PropertyGroup:表示GUID,编译名称,编译方式等 ItemGroup:
? ?Reference节点表示引用那些系统类库或第三方类库
? ?Compile节点表示编译那些类和资源文件
? ?Content表示图片,dll等资源文件
? ?ProjectReference引用其他项目说明
C#项目文件TestFileDefineDemoVS.csproj,使用记事本打开,我们发现本质是个xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4F66C7E6-56BC-4FB9-A9A0-3720089299E4}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>TestFileDefineDemoVS</RootNamespace>
<AssemblyName>TestFileDefineDemoVS</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Employee.cs" />
<Compile Include="JsonHelper.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="employee.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="dll\Newtonsoft.Json.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibraryTest\ClassLibraryTest.csproj">
<Project>{cf20e151-bae2-44d0-bddb-f67421f049ee}</Project>
<Name>ClassLibraryTest</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
|