【.NET】Add files into projects
Tags: dotnet
Published: 2021-02-25 11:20:33

## Intro
This time, I will try adding some files like .csproj, .dll, .resx, .json, and etc. 

### Environments
* .NET ver.5.0.103

## Add references to .csproj files
### Situations
1. There are two projects in a solution.
2. One is a xUnit project (AddFileSampleTest.csproj), another one is an ASP.NET Core project (AddFileSample.csproj).
3. I want to make the xUnit project refers the ASP.NET Core project.

### What I did
I executed this command.

```
dotnet add AddFileSampleTest reference AddFileSample
```

### Result
#### AddFileSampleTest.csproj
```xml
<Project Sdk="Microsoft.NET.Sdk">
...

  <ItemGroup>
    <ProjectReference Include="..\AddFileSample\AddFileSample.csproj" />
  </ItemGroup>

</Project>
```

* [dotnet add reference command - .NET CLI | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-add-reference)

## Add references to NuGet packages
### Situations
1. I want to make the ASP.NET Core project refers ClosedXML(ver.0.95)

### What I did
I executed this command.

```
dotnet add AddFileSample package ClosedXML -v 0.95
```

### Result
#### AddFileSample.csproj
```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
...
  <ItemGroup>
    <PackageReference Include="ClosedXML" Version="0.95" />
  </ItemGroup>
</Project>
```

* If I omitted "-v 0.95", the latest version package will be added.
* The case of package name is ignored. So I can write like "dotnet add AddFileSample package closedxml".
    But the case of the package name in the .csproj file also will be the same.

#### AddFileSample.csproj
```xml
...
  <ItemGroup>
    <PackageReference Include="closedxml" Version="0.95" />
  </ItemGroup>
...
```
* [dotnet add package command - .NET CLI | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-add-package)

## Add references to .dll files
Because I couldn't find to add references to .dll file, I edited the .csproj file directly.

#### AddFileSample.csproj
```xml
...
  <ItemGroup>
    <Reference Include="ClosedXML">
      <HintPath>Resources\ClosedXML.dll</HintPath>
    </Reference>
  </ItemGroup>
...
```

* For "HintPath", I can use both relative and absolute paths.

* [Referencing a .NET DLL directly, using the .NET Core toolchain | by Toni Solarin-Sodara | Medium](https://medium.com/@tonerdo/referencing-a-net-dll-directly-using-the-net-core-toolchain-16f0af46a4dc)

## Add references to .resx files
I don't need do anything to add references to .resx files.
After I created .resx files what were named like "Controllers.HomeController.en-us.resx", CLI would output "en-us" folder into bin/Debug/net5.0 directory.

To get strings from .resx files, I can use "IStringLocalizer", designer class, and etc. 

* [Globalization and localization in ASP.NET Core | Microsoft Docs](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0)
* [c# - How to get the .resx file strings in asp.net core - Stack Overflow](https://stackoverflow.com/questions/43181904/how-to-get-the-resx-file-strings-in-asp-net-core)

## Add references to .json(or .txt etc.) files
I just add the files into the project.
But I have to update the project file to include as an output file.

#### AddFileSample.csproj
```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
...
  <ItemGroup>
    <Content Update="nlog.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>
```