Dotnet Core 简明教程

.NET Core - Project Files

在本章中,我们将讨论 .NET Core 项目文件以及如何在项目中添加现有文件。

In this chapter, we will discuss .NET Core project files and how you can add existing files in your project.

让我们理解一个简单的例子。在该示例中,我们有某些已经创建的文件;我们必须在 FirstApp 项目中添加这些文件。

Let us understand a simple example in which we have some files which are already created; we have to add these files in our FirstApp project.

以下是 Student.cs 文件的实现。

Here is the implementation of the Student.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FirstApp {
   public class Student {
      public int ID { get; set; }
      public string LastName { get; set; }
      public string FirstMidName { get; set; }
      public DateTime EnrollmentDate { get; set; }
   }
}

以下是 Course.cs 文件的实现。

Here is the implementation of the Course.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FirstApp {
   public class Course {
      public int CourseID { get; set; }
      public string Title { get; set; }
      public int Credits { get; set; }
   }
}

现在让我们在磁盘和项目的源文件夹中保存这三个文件。

Let us now save these three files in your disk and the source folder of your project.

source folder
  1. Now if you are familiar with .NET and this one was a traditional .NET framework console application, it is important to understand how to add these files in your project in Visual Studio.

  2. You first need to drag the files to the solution explorer to copy them in your project folder, because your project needs reference to these files.

  3. One of the benefits of .NET Core is the approach taken with the project file (project.json); we can just drop files into the root of our project and then these will be automatically included in our project.

  4. We don’t have to manually reference files like we did in the past for traditional .NET Framework application in Visual Studio.

现在打开你的项目根目录。

Let us now open the root of your project.

root

将所有三个文件复制到项目根目录。

Let us now copy all of the three files into the root of your project.

project

你可以在根文件夹中看到所有复制的文件。

You can now see all the files copied to the root folder.

现在转到 Visual Studio,你会收到以下对话框。

Let us now go to Visual Studio; you will receive the following dialog box.

visual

单击 Yes to All 重新加载你的项目。

Click Yes to All to reload your project.

yes to all

现在,文件会被自动包含在你的项目中。

You will now that files are automatically included in your project.