我正在使用目标框架.NET 2.0在旧项目(Windows窗体应用程序)中工作。目标是构建一个.exe
不依赖任何配置文件的文件(尤其是不依赖的文件,.exe.config
因为应用程序不需要此配置文件中的任何内容)。
项目特点:
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
该解决方案中有一个app.config
文件,我曾尝试删除该文件,但由于某种原因,该文件似乎仍保留在项目中。当我说似乎保留在项目中是因为我遵循以下步骤:
App.config
,然后单击从项目中排除。.exe.config
生成。App.config
VS的应用程序配置文件时,显示以下消息:名称为'App.config'的文件已经存在。您要更换吗?。(我也尝试过右键单击App.config
,然后单击“删除”选项)
这似乎表明配置文件没有有效地从项目中删除,因此.exe.config
仍会生成。
另外,我检查了这篇文章,并尝试更改文件属性(“构建操作”,“复制到输出目录”等),但.exe.config
文件仍保留在构建中。
最后,我尝试过(可能更是一次绝望的尝试),取消选中“自动生成绑定重定向”,这会阻止生成,.exe.config
但.exe
不能正常运行。
就像其他信息一样,的内容.exe.config
是:
<?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>
我检查过的其他帖子:
如果需要任何其他信息,请告诉我进行更新。
禁用程序集绑定重定向的自动生成,而是处理AssemblyResolve
当前事件AppDomain.CurrentDomain
并在事件处理程序中加载请求的程序集。
禁用自动生成程序集绑定重定向
右键单击项目,选择“属性”,然后在项目属性页的第一个选项卡(“应用程序”选项卡)中,取消选中“自动生成绑定重定向”。您可以通过编辑项目文件和设置来执行相同操作<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
。
处理程序集解析事件
您应该在运行时尝试加载程序集之前添加事件处理程序。例如,在Program
类的静态构造函数中。例如,在下面的代码中,我假设应用程序正在寻找MyAssembly
我MyAssembly2.dll
在应用程序文件夹中拥有的文件。
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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句