Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

C# - 폴더 경로 문자열에서 "..", "." 표기를 고려한 최종 문자열을 얻는 방법

개인적으로 System.IO.Path.Combine 메서드의 사용을 꽤나 좋아합니다.

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
        string etcPath = Path.Combine(systemPath, "drivers", "etc");

        Console.WriteLine(etcPath); // C:\Windows\system32\drivers\etc
        Console.WriteLine(Directory.Exists(etcPath)); // True
    }
}

'\' 문자의 사용을 귀찮게 고려할 필요없이, 그냥 이름만 주면 알아서 척척 경로를 이어서 주기 때문인데요.

그런데, 가끔은 Path.Combine으로 부모 디렉토리로 이동하는 등의 경로 구성을 할 때 불편한 점이 있습니다.

// etcPath == C:\Windows\system32\drivers\etc
string winPath = System.IO.Path.Combine(etcPath, "..", "..", "..");
Console.WriteLine(winPath); // C:\Windows\system32\drivers\etc\..\..\..

보시는 것처럼, 출력 결과가 끝에 ".." 경로가 붙은 체로 반환되므로 별로 깔끔하지 않게 됩니다. 이럴 때 경로를 단일하게 구성하고 싶다면 DirectoryInfo 클래스의 도움을 받으면 됩니다.

DirectoryInfo dirInfo = new DirectoryInfo(winPath); // winPath == C:\Windows\system32\drivers\etc\..\..\..
Console.WriteLine(dirInfo.FullName); // C:\Windows




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 11/11/2014]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2014-11-12 06시11분
[leafbird] Path.GetFullPath(winPath) 를 사용해도 아마 동일하게 동작할겁니다.
[guest]
2014-11-12 06시27분
@leafbird 좋은 팁 감사합니다. ^^
정성태

... 181  182  183  184  185  186  187  188  189  190  191  192  193  194  [195]  ...
NoWriterDateCnt.TitleFile(s)
97정성태2/14/200522067VS.NET IDE: 21. 설치된 Platform SDK 버전확인 방법
96정성태2/14/200524010기타: 9. http://www.google.com/webhp?complete=1&hl=en 검색에 관해서.
95정성태2/14/200533328VS.NET IDE: 20. Win32 특권 정리 [1]
94정성태1/29/200524712VC++: 13. VS.NET 2005 VC++ 컴파일러 에러 : cannot instantiate abstract class
93정성태1/29/200520542VS.NET IDE: 19. 혹시 VS.NET 2005 Beta2(2004/12 CTP Team System 버전)이 다운이 자주 되나요? [2]
92정성태1/29/200519231.NET Framework: 23. Unmanaged 환경에서 Managed DLL에 정의된 메서드 호출 시 오류 확인하는 방법
91정성태11/14/200519858VC++: 12. VS.NET 2005 VC++ Debug: Expression: ( (state != ST_INVALID ) )
90정성태1/27/200520869.NET Framework: 22. Debug: The underlying connection was closed: Unable to connect to the remote server.
89정성태1/26/200525273VC++: 11. Delay Loaded DLL
87정성태1/23/200518894VS.NET IDE: 18. VS.NET 2005 Beta 1 - VC++ 프로젝트에서 Connection Point 구현시 버그
88정성태1/23/200518418    답변글 VS.NET IDE: 18.1. VS.NET 2003 : VC++ 프로젝트에서 Connection Point 추가시에도 버그
86정성태1/23/200524293.NET Framework: 21. Code Snippet - Enum과 관련된 다양한 형변환 [1]
85정성태1/23/200522488스크립트: 4. Windows 2003에서 BHO(Browser Helper Objects) 동작 안하는 현상 [1]
83정성태1/18/200527687.NET Framework: 20. System.AccessViolationException 예외가 발생한 한 예.
82정성태1/3/200521171VS.NET IDE: 17. Windows 운영 - 특정 사용자 또는 그룹에 대해서 파일 공유 접근 금지
79정성태1/20/200528944기타: 8. DELL Latitude D800 노트북 컴퓨터의 PC Beep 소음(!) 문제.
78정성태12/27/200421325VS.NET IDE: 16. MS 제품 관련 사용되는 TCP/IP 포트 열거파일 다운로드1
77정성태12/27/200421633VS.NET IDE: 15. Virtual CD-ROM Control Panel - ISO 이미지를 CD-ROM 드라이브처럼 접근하게 해주는 EXE 프로그램 [1]파일 다운로드1
76정성태12/27/200422677VS.NET IDE: 14. VPN 접속시 IP를 고정적으로 할당받는 방법 [1]
75정성태12/27/200418782VS.NET IDE: 13. VS.NET 2005 Beta 1 - Portfolio Explorer 에 등록된 Team Server 항목 삭제 방법
84정성태1/19/200519978    답변글 VS.NET IDE: 13.1. VS.NET 2005 Beta 1 : Team Server 에 등록된 포트폴리오 프로젝트 삭제 방법
74정성태12/26/200420286VS.NET IDE: 12. [시나리오] VS.NET 2005 Team Foundation Server을 Virtual Server에 설치 [1]
80정성태12/31/200419771    답변글 VS.NET IDE: 12.1. Client Tier, 즉 VS.NET 2005가 설치된 컴퓨터도 ActiveDirectory에 참여를 해야 합니다.
81정성태12/31/200421735    답변글 VS.NET IDE: 12.2. Tier 컴퓨터를 모두 영문으로 재구성
109정성태3/4/200516839    답변글 VS.NET IDE: 12.3. [보완] MS 공식 아티클 - Installing the December CTP Release of Visual Studio Team System
73정성태11/14/200518572.NET Framework: 19. VS.NET 2005 Team Foundation Server 설치오류 - 26204 예외
... 181  182  183  184  185  186  187  188  189  190  191  192  193  194  [195]  ...