简体   繁体   English

Visual Studio:如何在 exe 中嵌入 app.config?

[英]Visual Studio : how to embed app.config in the exe?

I have a program which compile and works fine and uses this app.config file:我有一个程序可以编译并正常工作并使用这个app.config文件:

<?xml version="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

When compiling the program it give me two files myapp.exe and myapp.exe.config .编译程序时,它给了我两个文件myapp.exemyapp.exe.config When starting the program if myapp.exe.config is missing the start will fail and an exception will be thrown启动程序时,如果缺少myapp.exe.config ,启动将失败并抛出异常

Mixed mode assembly is built against version 'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.混合模式程序集是针对运行时版本“v1.1.4322”构建的,如果没有其他配置信息,则无法在 4.0 运行时中加载。

Is there any way to include the config file in the application as a resource so I got only myapp.exe as an output?有没有办法将配置文件作为资源包含在应用程序中,所以我只有myapp.exe作为 output?

You can set useLegacyV2RuntimeActivationPolicy at runtime , the following codes are taken directly from that link.您可以在运行时设置 useLegacyV2RuntimeActivationPolicy ,以下代码直接取自该链接。 This helper class这个帮手 class

public static class RuntimePolicyHelper
{
    public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }

    static RuntimePolicyHelper()
    {
        ICLRRuntimeInfo clrRuntimeInfo =
            (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
                Guid.Empty, 
                typeof(ICLRRuntimeInfo).GUID);
        try
        {
            clrRuntimeInfo.BindAsLegacyV2Runtime();
            LegacyV2RuntimeEnabledSuccessfully = true;
        }
        catch (COMException)
        {
            // This occurs with an HRESULT meaning 
            // "A different runtime was already bound to the legacy CLR version 2 activation policy."
            LegacyV2RuntimeEnabledSuccessfully = false;
        }
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
    private interface ICLRRuntimeInfo
    {
        void xGetVersionString();
        void xGetRuntimeDirectory();
        void xIsLoaded();
        void xIsLoadable();
        void xLoadErrorString();
        void xLoadLibrary();
        void xGetProcAddress();
        void xGetInterface();
        void xSetDefaultStartupFlags();
        void xGetDefaultStartupFlags();

        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        void BindAsLegacyV2Runtime();
    }
}

will automatically setup on runtime, you can verify if RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully is true and handle the error otherwise.将在运行时自动设置,您可以验证 RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully 是否为真,否则处理错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM