.csproj 文件有什么作用?

发布于 2024-11-16 20:50:45 字数 54 浏览 2 评论 0原文

通常,C# 项目有一个与其关联的 .csproj 文件。那个文件是做什么用的?它包含什么数据?

Usually a C# project has a .csproj file associated with it. What is that file for? What data does it contain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回梦 2024-11-23 20:50:45

基本上,.csproj 文件包含项目中的文件列表,以及对系统程序集的引用等。

有一大堆设置 - Visual Studio 版本、项目类型、程序集名称、应用程序图标、目标区域性、安装 URL。 ..

构建项目所需的一切。虽然您可以假设您需要当前文件夹中的所有内容,但拥有明确的列表可以让您在磁盘和项目中对它们进行逻辑组织,以便您可以更轻松地找到所需的文件。

它只是 XML,因此您可以在您喜欢的文本编辑器中打开它并查看。

每个程序集都有一个 .csproj 文件,而 .sln(解决方案文件)将构成项目的所有程序集联系在一起。

Basically the .csproj file contains the list of files in your project, plus the references to system assemblies etc.

There are a whole bunch of settings - Visual Studio version, project type, Assembly name, Application Icon, Target Culture, Installation Url,...

Everything you need to build your project. While you could assume that you need everything in the current folder, having an explicit list allows you to organise them logically both on the disk and in the project so you can more easily find the files you want.

It's just XML so you can open it in your favourite text editor and take a look.

You get one .csproj file per assembly and the .sln (solution file) ties together all the assemblies that make up your project.

风透绣罗衣 2024-11-23 20:50:45

针对 .NET 5 进行了更新

这是我们应用程序中最重要的文件。它告诉 .NET 如何构建项目。

所有 .NET 项目都在 .csproj 文件中列出其依赖项。如果您以前使用过 JavaScript,请将其视为 package.json 文件。不同之处在于,这是一个 XML 文件,而不是 JSON。当您运行 dotnet Restore 时,它​​会使用此文件来确定要下载哪些 NuGet 包并将其复制到项目文件夹。

.csproj 文件还包含 .NET 工具构建项目所需的所有信息。它包括正在构建的项目的类型(控制台、Web、桌面等)、该项目的目标平台以及对其他项目或第三方库的任何依赖项。

从 .NET 5 开始,进行了一些重大更改,以使该文件更易于阅读和维护,如下所列。

  • 在旧版本的 .NET 中,项目中的每个文件都必须列在 .csproj 文件中。现在它们会被自动包含和编译。**
  • 以前,GUID 随处可见。现在它们很少被使用。
  • DLL 文件的路径已包含在内,但从 .NET 5 开始,您无需指定磁盘上的路径。

示例:

<!-- Sdk attribute specifies the type of project, which is web. -->
<Project Sdk="Microsoft.NET.Sdk.Web"> 

  <PropertyGroup>
    <!-- TargetFramework is the framework this project will be using, which in this case is .NET 5. -->
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <!-- PackageReference references the NuGet packages. -->
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />                                              
  </ItemGroup>

</Project>

注意:如果您使用的是 Visual Studio,此文件将被隐藏,但您可以右键单击该项目并选择编辑项目文件。

Updated for .NET 5

This is the most important file in our application. It tells .NET how to build the project.

All .NET projects list their dependencies in the .csproj file. If you have worked with JavaScript before, think of it like a package.json file. The difference is, instead of a JSON, this is an XML file. When you run dotnet restore, it uses this file to figure out which NuGet packages to download and copy to the project folder.

The .csproj file also contains all the information that .NET tooling needs to build the project. It includes the type of the project being built (console, web, desktop, etc.), the platform this project targets and any dependencies on other projects or 3rd party libraries.

Starting in .NET 5, there have been a few major changes to make this file easier to read and maintain, which are listed below.

  • In the older versions of .NET, every file in the project had to be listed in the .csproj file. Now they are automatically included and compiled.**
  • Previously, GUIDs were used everywhere. Now they are used rarely.
  • The path to the DLL files was included, but starting .NET 5, you don't need to specify the path on the disk.

Example:

<!-- Sdk attribute specifies the type of project, which is web. -->
<Project Sdk="Microsoft.NET.Sdk.Web"> 

  <PropertyGroup>
    <!-- TargetFramework is the framework this project will be using, which in this case is .NET 5. -->
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <!-- PackageReference references the NuGet packages. -->
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />                                              
  </ItemGroup>

</Project>

Note: If you are using Visual Studio, this file will be hidden, but you can right-click on the project and choose edit the project file.

数理化全能战士 2024-11-23 20:50:45

该文件包含要作为项目一部分进行编译的所有文件的列表,以及其他选项,例如项目名称、发布和调试配置、编译选项等。

The file contains a list of all the files to be compiled as part of the project, as well as other options like the project name, release and debug configurations, compilation options, and so on.

风吹雨成花 2024-11-23 20:50:45

摘自 什么是 .csproj 文件?

“.csproj”是 Visual Studio .NET C# 项目文件扩展名。该文件将包含有关该项目中包含的文件、该项目中使用的程序集、项目 GUID 和项目版本等的信息。该文件与您的项目相关。我们创建的时候会自动生成

“.sln”是 Visual Studio 中组织项目的结构。它包含 .sln(基于文本的共享)和 .suo(二进制、用户特定的解决方案选项)文件中项目的状态信息。我们可以在一个解决方案中添加多个项目。

Taken from What is .csproj file?

".csproj" is a Visual Studio .NET C# Project file extension. This file will have information about the files included in that project, assemblies used in that project, project GUID and project version etc. This file is related to your project. It will be automatically generated when we create

".sln" is a structure for organizing projects in Visual Studio. It contains the state information for projects in .sln (text-based, shared) and .suo (binary, user-specific solution options) files. We can add multiple projects inside one solution.

抚你发端 2024-11-23 20:50:45

它包含有关项目中使用的所有文件、使用的程序集(包括其他随后提供的程序集的路径)、输出类型、程序集名称等等的信息。因此,通过打开此 xml,您可以在下一个中找到所有信息屋顶。

It contains information about all the files used in the project, assemblies used(including the path for other then provided assemblies), output type, assembly name and much more.So, by opening this xml, you can find all the info in under one roof.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文