현재 코드가있는 어셈블리의 경로를 얻는 방법이 있습니까? 호출 어셈블리의 경로를 원하지 않고 코드를 포함하는 경로 만 원합니다.
기본적으로 단위 테스트는 dll과 관련된 xml 테스트 파일을 읽어야합니다. 테스트 dll이 TestDriven.NET, MbUnit GUI 또는 기타에서 실행되는지 여부에 관계없이 항상 경로가 올바르게 해결되기를 원합니다.
편집 : 사람들은 내가 요구하는 것을 오해하는 것 같습니다.
내 테스트 라이브러리는
C : \ projects \ myapplication \ daotests \ bin \ Debug \ daotests.dll
이 길을 가고 싶습니다 :
C : \ 프로젝트 \ myapplication \ daotests \ bin \ 디버그 \
MbUnit Gui에서 실행할 때 지금까지 세 가지 제안이 실패합니다.
-
Environment.CurrentDirectory
제공 \ 프로그램 파일 \ MbUnit에 C : -
System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location
제공 \ 문서 및 설정 \ 조지 \ 로컬 설정 \ 임시 \ …. \ DaoTests.dll : C -
System.Reflection.Assembly.GetExecutingAssembly().Location
이전과 동일합니다.
답변
단위 테스트에서이 속성을 자주 사용하므로 다음 속성을 정의했습니다.
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Assembly.Location
(어셈블리는 임시 폴더에서 실행) NUnit과를 사용하는 경우 속성은 가끔 사용하는 것을 선호하므로, 몇 가지 재미있는 결과를 제공합니다 CodeBase
당신에게 URI 형식으로 경로를 제공하고 UriBuild.UnescapeDataString
을 제거 File://
처음에, 그리고 GetDirectoryName
일반적인 윈도우 형식으로 변경 .
답변
도움이 되나요?
//get the full location of the assembly with DaoTests in it
string fullPath = System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location;
//get the folder that's in
string theDirectory = Path.GetDirectoryName( fullPath );
답변
다음과 같이 간단합니다.
var dir = AppDomain.CurrentDomain.BaseDirectory;
답변
John의 대답과 동일하지만 약간 덜 장황한 확장 방법입니다.
public static string GetDirectoryPath(this Assembly assembly)
{
string filePath = new Uri(assembly.CodeBase).LocalPath;
return Path.GetDirectoryName(filePath);
}
이제 할 수있는 일 :
var localDir = Assembly.GetExecutingAssembly().GetDirectoryPath();
또는 원하는 경우 :
var localDir = typeof(DaoTests).Assembly.GetDirectoryPath();
답변
CodeBase 및 UNC 네트워크 공유를 사용할 때 나를 위해 일한 유일한 솔루션은 다음과 같습니다.
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
일반 URI에서도 작동합니다.
답변
어셈블리가 섀도 복사 되지 않는 한 작동합니다 .
string path = System.Reflection.Assembly.GetExecutingAssembly().Location
답변
이건 어때?
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
