我需要将我的app.config文件嵌入到我的exe文件中,该文件只包含supportedRuntime设置。我尝试了构建动作嵌入式资源,但它现在没有从配置文件中读取值,它不起作用。这是我的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
所以我的想法是在.Net 4.0上运行我的.Net 2.0 exe。有任何想法吗?
感谢。
答案 0 :(得分:3)
这是不可能的。如果你绝对必须有没有配置文件的可执行文件,那么最接近的就是编写将为你运行CLR的非托管加载程序。
假设你有c#app喜欢:
using System;
namespace DumpVersion
{
class Program
{
static int EntryPoint(string argument)
{
Console.Out.WriteLine(argument);
Console.Out.WriteLine(Environment.Version);
Console.In.ReadLine();
return 0;
}
static void Main()
{
EntryPoint("Main");
}
}
}
您可以创建非托管(c++)加载程序,如:
#include <metahost.h>
#pragma comment(lib, "mscoree.lib")
#import "mscorlib.tlb" raw_interfaces_only \
high_property_prefixes("_get","_put","_putref") \
rename("ReportEvent", "InteropServices_ReportEvent")
int wmain(int argc, wchar_t* argv[])
{
HRESULT hr;
ICLRMetaHost *pMetaHost = NULL;
ICLRRuntimeInfo *pRuntimeInfo = NULL;
ICLRRuntimeHost *pClrRuntimeHost = NULL;
// build runtime
// todo: add checks for invalid hr
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&pMetaHost));
hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo));
if (hr != S_OK) {
hr = pMetaHost->GetRuntime(L"v2.0.50727", IID_PPV_ARGS(&pRuntimeInfo));
}
hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost,
IID_PPV_ARGS(&pClrRuntimeHost));
// start runtime
hr = pClrRuntimeHost->Start();
// execute managed assembly
DWORD pReturnValue;
hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(
L"c:\\temp\\TestLoading\\DumpVersion\\bin\\Debug\\DumpVersion.exe",
L"DumpVersion.Program",
L"EntryPoint",
L"hello .net runtime",
&pReturnValue);
// free resources
pMetaHost->Release();
pRuntimeInfo->Release();
pClrRuntimeHost->Release();
return 0;
}
更多信息:https://www.codeproject.com/Articles/607352/Injecting-Net-Assemblies-Into-Unmanaged-Processes