[c#] 현재 실행중인 DLL의 위치를 ​​얻는 방법은 무엇입니까?

작성중인 dll 실행의 일부로로드해야하는 구성 파일이 있습니다.

내가 가진 문제는 내가 dll 및 구성 파일을 넣은 위치가 앱이 실행될 때 “현재 위치”가 아니라는 것입니다.

예를 들어, 여기에 dll 및 xml 파일을 넣습니다.

D : \ Program Files \ Microsoft Team Foundation Server 2010 \ Application Tier \ Web Services \ bin \ Plugins

그러나 다음과 같이 (내 dll에있는) xml 파일을 참조하려고하면 :

XDocument doc = XDocument.Load(@".\AggregatorItems.xml")

. \ AggregatorItems.xml은 다음과 같이 변환됩니다.

C : \ windows \ system32 \ inetsrv \ AggregatorItems.xml

따라서 현재 실행중인 dll이 어디에 있는지 아는 방법을 찾아야합니다. 기본적으로 나는 이것을 찾고 있습니다.

XDocument doc = XDocument.Load(CoolDLLClass.CurrentDirectory+@"\AggregatorItems.xml")



답변

당신이 찾고있는 System.Reflection.Assembly.GetExecutingAssembly()

string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string xmlFileName = Path.Combine(assemblyFolder,"AggregatorItems.xml");

노트 :

.Location속성은 현재 실행중인 DLL 파일의 위치를 ​​반환합니다.

일부 조건에서 DLL은 실행 전에 섀도 복사되며 .Location속성은 복사본의 경로를 반환합니다. 원래 DLL의 경로를 원하는 경우 Assembly.GetExecutingAssembly().CodeBase대신 속성을 사용하십시오.

.CodeBasefile:\제거해야 할 수 있는 접두사 ( ) 가 포함되어 있습니다 .


답변

지적했듯이 성찰은 당신의 친구입니다. 그러나 올바른 방법을 사용해야합니다.

Assembly.GetEntryAssembly()     //gives you the entrypoint assembly for the process.
Assembly.GetCallingAssembly()   // gives you the assembly from which the current method was called.
Assembly.GetExecutingAssembly() // gives you the assembly in which the currently executing code is defined
Assembly.GetAssembly( Type t )  // gives you the assembly in which the specified type is defined.


답변

필자의 경우 (Outlook에 [파일로]로드 된 어셈블리 처리) :

typeof(OneOfMyTypes).Assembly.CodeBase

에서 CodeBase(아님 Location) 의 사용에 유의하십시오 Assembly. 다른 사람들은 어셈블리를 찾는 다른 방법을 지적했습니다.


답변

System.Reflection.Assembly.GetExecutingAssembly().Location


답변

asp.net 응용 프로그램으로 작업 중이고 디버거를 사용할 때 어셈블리를 찾으려면 일반적으로 임시 디렉터리에 저장됩니다. 나는 그 시나리오를 돕기 위해이 방법을 썼다.

private string[] GetAssembly(string[] assemblyNames)
{
    string [] locations = new string[assemblyNames.Length];


    for (int loop = 0; loop <= assemblyNames.Length - 1; loop++)
    {
         locations[loop] = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic && a.ManifestModule.Name == assemblyNames[loop]).Select(a => a.Location).FirstOrDefault();
    }
    return locations;
}

자세한 내용은이 블로그 게시물 http://nodogmablog.bryanhogan.net/2015/05/finding-the-location-of-a-running-assembly-in-net/을 참조하십시오.

소스 코드를 변경하거나 재배포 할 수 없지만 컴퓨터에서 실행중인 프로세스를 검사 할 수있는 경우 Process Explorer를 사용합니다. 여기에 자세한 설명을 썼습니다 .

시스템에서 실행중인 모든 dll을 나열하고 실행중인 응용 프로그램의 프로세스 ID를 확인해야 할 수도 있지만 일반적으로 그렇게 어렵지는 않습니다.

IIS 내부의 dll에 대한 전체 설명을 작성했습니다 -http://nodogmablog.bryanhogan.net/2016/09/locating-and-checking-an-executing-dll-on-a-running-web -섬기는 사람/


답변