WinForm 응용프로그램의 실행 파일 경로는 System.Windows.Forms 네임스페이스에 있는 Application.StartupPath 프로퍼티를 참조하면 쉽게 구할 수 있다.

using System.Windows.Forms;

...

string path = Application.StartupPath;


 클래스 라이브러리가 WinForm 모듈이 아닐 경우에는 System.Reflection 네임스페이스에 있는 Assembly 클래스를 이용하면 된다. Assembly.GetEntryAssembly().Location 프로퍼티를 이용하면 실행 파일 이름을 포함한 전체 경로를 얻게 되므로 Path 클래스를 이용하면 쉽게 구할 수 있다.

using System.Reflection;

...

string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

 Assembly.GetEntryAssembly().Location MSDN 설명을 보면 클래스 라이브러리 즉, 관리되는 코드를 관리되지 않는 코드에서 호출할 경우 null을 리턴한다고 나와있다. 이것만 주의해 주면 되겠다.

Posted by six605
,