2016-11-08 4 views
1

TL 알 수없는입니다실행 조립 위치

DR

System.Reflection.Assembly.GetExecutingAssembly().Location 

반환 "<Unknown>"를?

전체 이야기 : 콘솔 응용 프로그램을 수동으로 실행하면 모든 것이 잘됩니다. 내 애플 리케이션을 실행하는 작업 스케줄러를 설정할 때 문제가있다 : 내 애플 리케이션은 일부 종속 파일을 찾을 수 없습니다. 이러한 파일은 .config 파일에 다음과 같이 작성됩니다. <add key="SomeFile" value="SomeFolder\SomeFile.xml"/>, 상대 pathes 만 필요합니다.

작업 스케줄러에서 내 응용 프로그램이 system32에있는 taskeng.exe에서 실행되기 때문에 '현재 폴더'= '응용 프로그램 폴더'를 수동으로 실행하지만 작업 스케줄러로 실행하면 '현재 폴더'= C:\Windows\system32이됩니다. 이 답변 https://stackoverflow.com/a/16102640/6184866에서

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); 

:

그래서 조립 경로를 찾기 위해이 코드를 사용하고 싶습니다.

하지만 작동하지 않습니다.

+0

당신은 Directory.GetCurrentDirectory를 몇 가지 변수에서 동일하게 가져올 수 있습니다. 그런 다음 Directory.SetCurrentDirectory를 필요에 따라 다시 설정합니다. 작업을 완료하면 현재 디렉토리를 다시 temp로 설정합니다. –

답변

0

콘솔 응용 프로그램에서도 동일한 문제가있었습니다. 때

내가에만이 오류가 발생 :

  1. 조립이 프로그램은 비주얼 스튜디오의 외부에서 실행되고 프로세스는
  2. 명령에 부착되어
  3. 에 최적화와 자료로 구축 Assembly.GetExecutingAssembly().Location 직접 실행 창을 통해 실행됩니다.

다음 코드를 사용하여 복제 할 수 있습니다. 루프의 잠은 디버거를 연결할 시간을 주기만하면됩니다.

static void Main(string[] args) 
    { 
     for (int i = 0; i < 10; i++) 
     { 
      Console.WriteLine(10 - i); 
      Thread.Sleep(1000); 
     } 

     Console.WriteLine(Assembly.GetExecutingAssembly().Location); 
     Console.WriteLine(Directory.GetCurrentDirectory()); 

     Console.ReadLine(); 
    } 

명령을 Assembly.GetExecutingAssembly().Location 반환 <Unknown>를 실행 코드 (위의 제약 조건에 해당하는 경우) 및 직접 실행 창에서를 Breakpointing. Directory.GetCurrentDirectory()을 실행하면 Cannot evaluate expression because the code of the current method is optimized.

이 반환되지만 예상되는 올바른 값이 실행되고 콘솔에 인쇄됩니다.