DEV Community

loading...

【.NET】Add files into projects

Masui Masanori
Programmer, husband, father I love C#, TypeScript, etc.
・2 min read

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
Enter fullscreen mode Exit fullscreen mode

Result

AddFileSampleTest.csproj

<Project Sdk="Microsoft.NET.Sdk">
...

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

</Project>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Result

AddFileSample.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">
...
  <ItemGroup>
    <PackageReference Include="ClosedXML" Version="0.95" />
  </ItemGroup>
</Project>
Enter fullscreen mode Exit fullscreen mode
  • 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

...
  <ItemGroup>
    <PackageReference Include="closedxml" Version="0.95" />
  </ItemGroup>
...
Enter fullscreen mode Exit fullscreen mode

Add references to .dll files

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

AddFileSample.csproj

...
  <ItemGroup>
    <Reference Include="ClosedXML">
      <HintPath>Resources\ClosedXML.dll</HintPath>
    </Reference>
  </ItemGroup>
...
Enter fullscreen mode Exit fullscreen mode

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.

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

<Project Sdk="Microsoft.NET.Sdk.Web">
...
  <ItemGroup>
    <Content Update="nlog.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>
Enter fullscreen mode Exit fullscreen mode

Discussion (0)