How to publish as a separate file without being included in a single-run file

Page update date :
Page creation date :

Environment

Visual Studio
  • Visual Studio 2019
ASP.NET Core
  • 3.1

What is a single file?

In addition to the exe file, you will often need a file such as a dll to run a program. You will distribute multiple files and programs.

You can now run all the files in one exe by publishing the program using the feature "Create Single File" added from .NET Core 3.0. (The pdb file is a debug information file, so you don't have to distribute it.)

However, in some cases, you may want to place a configuration file in a folder with exe instead of including it in the exe. This section describes how to do this.

Preparing for single-file output

As a sample, the following project configuration should be used to load the TextFile1.txt file at startup. TextFile1.txt is intended to be distributed with the exe file, so make it "Build Action: None" and "Copy to Output Directory: Copy to New If New." The contents of TextFile1.txt can be arbitrary.

You can build a program with the assumption that you want to load files that are distributed together.

However, the executable file in a single file is actually expanded to a separate temporary folder when it is run. When you load a file in a relative path, you must specify it in the folder where you started the executable file, not in the folder where the executable file is located.

using System;
using System.Diagnostics;
using System.IO;

namespace ExcludeFromSingleFile
{
  class Program
  {
    static void Main(string[] args)
    {
      using var processModule = Process.GetCurrentProcess().MainModule;
      Console.WriteLine(File.ReadAllText(Path.Combine(Path.GetDirectoryName(processModule.FileName), "TextFile1.txt")));
      Console.ReadKey();
    }
  }
}

When you debug, you can see that the file is loaded.

TextFile1.txt is also printed in the output folder because it has not yet been compiled into a single file.

Let's publish it as a single file in this state. The procedure is omitted because the publication is not intended.

The target runtime must be non-portable to be a single file.

I haven't set anything yet, so it's one file as usual.

There is no TextFile1.txt when you run it, so you will get an error.

How to publish a specific file without including it in a single file

Open the project file (csproj) in code with the file that you do not want to include in the project.

I think it's xml like this: Look for the file for which you want to change settings. Depending on your project settings, you may not have a target file, so you may need to add it manually.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Update="TextFile1.txt">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

When you find the file, add ExcludeFromSingleFile with true, as follows: As the name implies, it is excluded from a single file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Update="TextFile1.txt">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <!-- ここから追加 -->
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      <!-- ここまで追加 -->
    </None>
  </ItemGroup>
</Project>

Please try to publish once you have saved. You can see that it is published out of a single file.

You can still run the published file to verify that it is working correctly.