C\#현재 디 렉 터 리 에서 이전 디 렉 터 리 를 찾 을 수 있 습 니 다.

1699 단어 C#웃 층목차
사실 간단 하고 머리 가 없 지만 실 용적 입 니 다.바로 문자열 을 뜯 는 방법 을 사용 하 는 것 입 니 다.

/// <summary>
    ///         
    /// </summary>
    /// <returns></returns>
    public string GetProjectRootPath()
    {
      string rootPath = "";
      string BaseDirectoryPath = AppDomain.CurrentDomain.BaseDirectory; // F:\project\WPF\AstroATE-PDR\04.   \01.    \AstroATE\AstroATE\bin\Debug
      //       ,       
      rootPath = BaseDirectoryPath.Substring(0, BaseDirectoryPath.LastIndexOf("\\")); //    \    ,      
      rootPath = rootPath.Substring(0, rootPath.LastIndexOf(@"\"));  //         
      rootPath = rootPath.Substring(0, rootPath.LastIndexOf("\\")); // @"F:\project\WPF\AstroATE-PDR\04.   \01.    \AstroATE\AstroATE
      return rootPath;
    }
이 함수 호출:

string str = GetProjectRootPath() + @"\data\    .pdf";  //        
자,이렇게 하면 해결 된다.
추가:C\#실행 가능 한 파일 경로 의 상위 디 렉 터 리 를 가 져 오 는 방법
첫 번 째 종류:

DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", Application.StartupPath)); 
di.FullName
몇 개 는 몇 층 으로 되 돌아 가 는 거 예요.
두 번 째:

DirectoryInfo info = new DirectoryInfo(Application.StartupPath); 
String path = info.Parent.Parent.FullName;
세 번 째:

string WantedPath = Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf(@"\"));
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기