2014-12-15 1 views
2

구조 :- \bin\debug\abc.exe\Libs\win32\xyz.dll입니다. abc.exe을 실행하려면 xyz.dll을 참조해야합니다. 나는 app.config에서 "probing"태그로 시도했지만이 경우 이있는 곳에 debug '폴더에 'Libs'폴더가있는 경우에만 가능성이있었습니다. 하지만 .exe에서 2 개의 폴더를 가져온 다음 \Libs\win32으로 이동하여 .dll을 참조하고 싶습니다. 내가 어떻게해야하는지 제안 해주세요.는 .net dll을 exe 디렉토리가 아닌 다른 폴더에서 가져옵니다.

+0

리플렉션을 사용하여 어셈블리로드 –

+0

이런 종류의 DLL을 호출하는 것은 의도적으로 매우 현명하지 않으며 CLR은 발을 쏠 때 피하는 데 도움이되도록 최선을 다합니다. 이를 무효로하려면 AppDomain.AssemblyResolve 이벤트가 필요하며 해당 이벤트의 이벤트 처리기에서 Assembly.LoadFrom()을 사용합니다. 아직 Main() 메소드에서 필요하지 않은지 확인하십시오. –

답변

0

파일 경로에서 .. \를 사용하여 디렉토리를 위로 이동합니다. 당신이 abc.exe 빈 \ 디버그 \ \ 에 있다면 프로젝트를 빌드 할 때
그럼 \ libs와 \의는 Win32에 대한 참조는 \ xyz.dll

..\..\Libs\win32\xyz.dll 

이 만 필요합니다 될 것입니다 , 당신의 실행 파일이 dll을 올바르게 참조하고 있다면 빌드 될 때 dll과 같은 폴더에 넣기 만하면됩니다.

물론 dllimport 또는 런타임 중에 dll의 정확한 경로를 알아야하는 경우를 제외하고는.

+0

하지만이 경로는 어디에 사용해야합니까? app.confog의 태그는 하위 폴더 만 참조 할 수 있기 때문에 이러한 경로와 작동하지 않습니다. – user3743646

+0

아니요. exe를 실행할 때만 다른 폴더의 DLL을 참조해야합니다. – user3743646

+0

이 DLL을 정확히 사용하는 방법은 일반 .NET 프로젝트 참조 또는 Win32 dll 또는 다른 것입니까? 빌드 할 때 같은 폴더 나 하위 폴더에 복사하지 않는 이유가 있습니까? – Nanhydrin

3

하나의 옵션이 AssemblyResolve 이벤트를 처리하고, 때마다 .NET은이 AssemblyResolve 이벤트를 트리거 전류 경로의 조립이 필요 찾을 수 없습니다 :

{ 
    // Execute in startup 
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve; 
} 

private Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) 
{ 
    string RESOURCES = ".resources"; 
    try 
    { 
     /* Extract assembly name */ 
     string[] sections = args.Name.Split(new char[] { ',' }); 
     if (sections.Length == 0) return null; 

     string assemblyName = sections[0]; 

     /* If assembly name contains ".resource", you don't need to load it*/ 
     if (assemblyName.Length >= RESOURCES.Length && 
       assemblyName.LastIndexOf(RESOURCES) == assemblyName.Length - RESOURCES.Length) 
     { 
      return null; 
     } 

     /* Load assembly to current domain (also you can use simple way to load) */ 
     string assemblyFullPath = "..//..//Libs//" + assemblyName; 
     FileStream io = new FileStream(assemblyNameWithExtension, FileMode.Open, FileAccess.Read); 
     if (io == null) return null; 
     BinaryReader binaryReader = new BinaryReader(io); 
     Assembly assembly = Assembly.Load(binaryReader.ReadBytes((int)io.Length)); 

     return assembly; 
    } 
    catch(Exception ex) 
    {} 
} 

* 또 다른 옵션은 현재에 필요한 모든 어셈블리를로드 도메인을 시작하십시오.

관련 문제