3

I'm working in a legacy project (Windows Forms Application), with target framework .NET 2.0. The goal is to build a single .exe file that does not depend in any configuration file (in particular that does not depend on the .exe.config because the application doesn't need anything from this configuration file).

The characteristics of the project:

Application:
Targe framework: .NET Framework 2.0
Output type: Windows Application
Auto-generate binding redirecs: Not enabled

Build:
Platform target: Any CPU

Visual Studio Community 2019, version 16.1.3

The solution has a app.config file that I've tried to remove but for some reason It seems that remains in the project. When I say seems to remains in the project is because I follow this steps:

  1. In the Solution Explorer, right-click on the App.config, and then click on Exclude From Project.
  2. Rebuild the project, and still the .exe.config in generated.
  3. Again in the Solution Explorer > Add > New Item, and when I try to add an Application Configuration File with the name App.config VS show this message: A file with the name 'App.config' already exists. Do you want to replace it?.

(I've also tried with the right-click on the App.config, and then click Delete option)

This seems to indicate that the configuration file is not effectively removed from the project, and therefore the .exe.config is still generated.

Also, I've checked this post, and tried changing the files properties (Build Action, Copy to Output Directory, etc) but the .exe.config file remains in the build.

Finally, I've tried (probably more as a desperate attempt) with uncheck the Auto-generate binding redirects which prevents the generation of the .exe.config but the .exe is not running properly.

Just as an additional information, the content of the .exe.config is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Company.Afis.ImageContainer" publicKeyToken="97cac07b8409e999" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.5.0.1827" newVersion="2.5.0.1827" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Other post that I've checked:

If any other information is needed, please let me know to update it.

Pete Ythong
  • 305
  • 5
  • 13
Alejandro Montilla
  • 2,626
  • 3
  • 31
  • 35
  • 1
    You can disable assembly bindings and use [`AppDomain.AssemblyResolve`](https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.assemblyresolve?view=netframework-4.8). – Reza Aghaei Aug 16 '19 at 10:07
  • How about pdb-files? Do you have them disabled? I am asking because I am not sure why do you want to disable exe.config-files. To have *clean* `Bin` folder with just one exe inside? Does other files cause problem? Which problem? – Sinatr Aug 16 '19 at 10:08
  • @Reza Aghaei I'm checking that, thanks. @Sinatr, I've enable/disable Debug info, but it does not affect the `.exe.config`. And the Idea is to have a `.exe` file that does not depend on any other file, so yes.. to have a clean `Bin` folder. As far as problems, well if I delete the `.exe.config` file the `.exe` is not running properly. – Alejandro Montilla Aug 16 '19 at 10:15

1 Answers1

2

Disable auto generation of assembly binding redirects and instead, handle AssemblyResolve event of the current AppDomain.CurrentDomain and load the requested assembly in the event handler.

  • Disable Auto-generate assembly binding redirects

    Right click on project, choose Properties and in the project properties page, in the first tab (Application tab), uncheck Auto-generate binding redirects. You can do the same by editing project file and setting <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>.

  • Handle AssemblyResolve Event

    You should add the event handler before the run-time tries to load the assembly. For example, in the static constructor of Program class. For example, in the following code, I suppose the application is looking for MyAssembly which I have it as MyAssembly2.dll in application's folder.

    static Program()
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }
    private static Assembly CurrentDomain_AssemblyResolve(object sender, 
        ResolveEventArgs args)
    {
        if (new AssemblyName(args.Name).Name == "MyAssembly")
            return Assembly.LoadFrom(
                Path.Combine(Application.StartupPath, "MyAssembly2.dll"));
        throw new Exception();
    }
    
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398