A colossal Dreamer: GR鐵塔-天生我材

[윈도구:ASP.NET] 페이지에서 물리적인 위치 획득(How to get Physical path) 본문

Development/막-윈도그

[윈도구:ASP.NET] 페이지에서 물리적인 위치 획득(How to get Physical path)

江多林 2009. 11. 11. 16:58

웹프로그래밍을 하다 보면, 가끔 파일을 저장해야하는 경우가 있습니다.

상대 위치에 대한 서버내의 물리적인 위치의 획득은 System.Web.HttpServerUtility 의 MapPath 메소드로 획득 할 수 있습니다.

그런데, HttpServerUtility 객체를 임의로 생성할 수 없습니다. 만...

사실은 매우 간단하게 획득하여 사용할 수 있습니다.
System.Web.UI.Page 객체내에 Server 프로퍼티가 있습니다.
Server 프로퍼티는 HttpServerUtility 인스턴스이고요..


다시 말해서 모든 웹페이지는 System.UI.Page를 상속 받으므로
페이지 코드에서 아래 코드로 서버내의 물리적인 위치를 얻을 수 있습니다.

string strCurrentPath = Server.MapPath("."); // 현재 위치의 물리적인 위치를 획득하는 경우
string strWebRootPath = Server.MapPath("\\."); // 웹루트의 물리적인 위치를 획득하는 경우

현재 페이지의 URL이 http://localhost/WebApplication.Server/Admin/test.aspx 일 때..

strCurrentPath ==> D:\WebApplication.Server\Admin
    // ===> WebApplication.Server 가상디렉토리의 물리적인 위치 + Admin
strWebRootPath ==> C:\inetpub\wwwroot\
    // ===> IIS 의 기본 root 디렉토리

이렇게 나옵니다.

--------------------------------------------------------------------------------
Always Any time Everytime think about who remember U. - Top Chul