itsource

현재 어셈블리의 경로를 가져오는 중

mycopycode 2023. 5. 27. 11:09
반응형

현재 어셈블리의 경로를 가져오는 중

현재 어셈블리의 경로를 가져오려면 어떻게 해야 합니까?hte current assembly(.dll)의 위치와 관련된 일부 경로에서 데이터를 가져와야 합니다.

누가 반사 네임스페이스를 사용하라고 한 줄 알았는데, 아무것도 안 보이네요.

사용할 수 있는 항목:

string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

댓글에 있는 몇 가지 제안은 그것을 전달하는 것입니다.System.Uri.UnescapeDataString (vvnurmi에서) 백분율 할당이 처리되는지 확인하고 사용합니다.Path.GetFullpath (TrueWill에서) 백슬래시 대신 슬래시를 사용하는 대신 경로가 표준 Windows 형식인지 확인합니다.다음은 각 단계에서 얻을 수 있는 결과의 예입니다.

string s = Assembly.GetExecutingAssembly().CodeBase;
Console.WriteLine("CodeBase: [" + s + "]");
s = (new Uri(s)).AbsolutePath;
Console.WriteLine("AbsolutePath: [" + s + "]");
s = Uri.UnescapeDataString(s);
Console.WriteLine("Unescaped: [" + s + "]");
s = Path.GetFullPath(s);
Console.WriteLine("FullPath: [" + s + "]");

실행 중인 경우 출력C:\Temp\Temp App\bin\Debug\TempApp.EXE:

CodeBase: [file://C:/Temp/TempApp/bin/Debug/TempApp.EXE]AbsolutePath : [C:/Temp/Temp%20App/bin/Debug/TempApp.EXE]탈출 취소: [C:/Temp/TempApp/bin/Debug/TempApp]EXE]전체 경로: [C:\Temp\Temp App\bin\Debug\TempApp.EXE]

System.Reflection.Assembly.GetExecutingAssembly().Location

분명히 말씀드리자면,

Assembly.GetExecutingAssembly().Location

선호합니다

new System.Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;

ExcidedCodeBase는 로컬 경로에 잘못된 URI 문자가 있을 수 있는 시나리오를 다룹니다(https://stackoverflow.com/a/21056435/852208) 참조).

LocalPath는 로컬 경로와 unc 경로 모두에 대한 전체 경로를 포함하며, AbsolutePath는 "\\server"를 트리밍합니다.

언급URL : https://stackoverflow.com/questions/864484/getting-the-path-of-the-current-assembly

반응형