2017-03-10 8 views
0

리소스에서 EXE 파일을 실행하는 방법에 대해 실험하고 싶습니다.Assembly.Load - throw 된 예외 : mscorlib.dll의 'System.BadImageFormatException'

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 {"Could not load file or assembly '59088 bytes loaded from hm_1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."} System.Exception {System.BadImageFormatException}

내가 64 플랫폼 모드와 viceversa에에 86를 실행하는 방법에 대한 특정 게시물을 읽고, 여전히 같은 오류가 비주얼 스튜디오에서 변경하고 : 내 코드를 디버깅 할 때

그러나, 나는 오류가 발생합니다.

누구나 아이디어가 있습니까? 참고 : 리소스를 로컬에서 실행하기 위해서만 로컬로 파일을 만들고 싶지 않습니다.

+0

포함 된 파일 대신 별도의 파일로 테스트 할 수 있습니까? –

+0

Works 완벽하게 Process.Start(). 그러나 자원으로 처리하고 싶습니다. – ItayNG

+0

[추출] (http://stackoverflow.com/questions/13031778/how-can-i-extract-a-file-from-an-embedded-resource-and-save-it-to-disk)) 자원을 임시 파일에로드 한 다음로드하십시오. –

답변

0

코드는 관리되는 앱에서만 작동합니다. 올바른 방법은 자원에서 관리 응용 프로그램을 실행합니다 :

// 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); 

을하지만 자원 관리되지 않는 응용 프로그램이있는 경우, 당신은 어셈블리로로드은`t. 임시 폴더에 '.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(); 
+0

대단히 감사합니다 :) 그건 내가 생각한 것입니다. – ItayNG