Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-Assembly.Load-抛出异常:';System.BadImageFormatException';在mscorlib.dll中_C#_.net_Resources_Mscorlib_Assembly.load - Fatal编程技术网

C#-Assembly.Load-抛出异常:';System.BadImageFormatException';在mscorlib.dll中

C#-Assembly.Load-抛出异常:';System.BadImageFormatException';在mscorlib.dll中,c#,.net,resources,mscorlib,assembly.load,C#,.net,Resources,Mscorlib,Assembly.load,我想做一个关于从资源运行EXE文件的实验 Assembly a = Assembly.Load(hm_1.Properties.Resources.HashMyFiles); MethodInfo method = a.EntryPoint; if (method != null) { method.Invoke(a.CreateInstance("a"), null); } **对于这个实验,我使用了一个名为HashMyFiles.exe的文件,它位于我的参考资料中 但是,当我调试

我想做一个关于从资源运行EXE文件的实验

Assembly a = Assembly.Load(hm_1.Properties.Resources.HashMyFiles);
MethodInfo method = a.EntryPoint;
if (method != null)
{
     method.Invoke(a.CreateInstance("a"), null);
}
**对于这个实验,我使用了一个名为HashMyFiles.exe的文件,它位于我的参考资料中

但是,当我调试代码时,会出现以下错误:

ex{“无法加载文件或程序集”从HMU 1、版本=1.0.0.0、区域性=neutral、PublicKeyToken=null或其依赖项之一加载59088字节。试图加载格式不正确的程序。}System.Exception{System.BadImageFormatException}

我读了一些关于在x64平台模式和viceversa上运行x86的帖子,在visual Studio中对其进行了更改,但仍然出现了相同的错误

有人有主意吗?
注意:我不想在本地创建文件,只想从资源运行它

您的代码仅适用于托管应用程序。 从资源运行托管应用程序的正确方法:

// You must change 'Properties.Resources.TestCs' to your resources path
// You needed to use 'Invoke' method with 'null' arguments, because the entry point is always static and there is no need to instantiate the class.
Assembly.Load(Properties.Resources.TestCs).EntryPoint.Invoke(null, null);
但如果资源中有非托管应用程序,则无法将其作为程序集加载。您应该将其另存为“.exe”文件到临时文件夹,并作为新进程运行。此代码适用于所有类型的应用程序(托管和非托管)


是否可以使用单独的文件而不是嵌入的文件进行测试?与Process.Start()完美配合。但是,我想用一个资源来做。你最好把资源放到一个临时文件中,然后再加载。这是我以前的实验,成功了。我仍在从我的资源中寻找一种方法……嗨,@ItayNG。我将您的代码复制到我的测试解决方案中,并将.exe文件添加到资源中。在运行时,我成功地从资源加载并运行了.exe文件。我的.exe文件是在C#上编写的。您的.exe文件是托管文件还是非托管文件?您在参考资料中使用什么编程语言编写和编译.exe?非常感谢:)这就是我的想法。
// Generate path to temporary system directory.
var tempPath = Path.Combine(Path.GetTempPath(), "testcpp.exe");

// Write temporary file using resource content.
File.WriteAllBytes(tempPath, Properties.Resources.TestCpp);

// Create new process info
var info = new ProcessStartInfo(tempPath);

// If you need to run app in current console context you should use 'false'.
// If you need to run app in new window instance - use 'true'
info.UseShellExecute = false;

// Start new system process
var proccess = Process.Start(info);

// Block current thread and wait to end of running process if needed
proccess.WaitForExit();