태그 보관물: c#

c#

전체 파일 경로에서 폴더 이름 가져 오기 얻습니까? 이것은 아래 파일 경로입니다. c:\projects\root\wsdlproj\devlop\beta2\text 여기서 “텍스트”는 폴더 이름입니다. 이

응용 프로그램의 전체 경로에서 폴더 이름을 어떻게 얻습니까?

이것은 아래 파일 경로입니다.

c:\projects\root\wsdlproj\devlop\beta2\text

여기서 “텍스트”는 폴더 이름입니다.

이 경로에서 폴더 이름을 어떻게 얻을 수 있습니까?



답변

DirectoryInfo.Name 참조 :

string dirName = new DirectoryInfo(@"c:\projects\roott\wsdlproj\devlop\beta2\text").Name;


답변

파일 경로에서 상위 폴더 이름을 얻고 싶다고 생각합니다. 쉽게 얻을 수 있습니다. 한 가지 방법은 FileInfo유형 객체 를 만들고 해당 Directory속성을 사용하는 것 입니다.

예:

FileInfo fInfo = new FileInfo("c:\projects\roott\wsdlproj\devlop\beta2\text\abc.txt");

String dirName = fInfo.Directory.Name;


답변

이 시도

var myFolderName = @"c:\projects\roott\wsdlproj\devlop\beta2\text";
var result = Path.GetFileName(myFolderName);


답변

이것을 사용할 수 있습니다.

string path = @"c:\projects\roott\wsdlproj\devlop\beta2\text";
string lastDirectory = path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Last();


답변

간단히 사용 Path.GetFileName

여기-폴더의 전체 경로에서 폴더 이름을 추출합니다.

string folderName = Path.GetFileName(@"c:\projects\root\wsdlproj\devlop\beta2\text");//Return "text"

다음은 몇 가지 추가 사항입니다. 파일의 전체 경로에서 폴더 이름을 추출합니다.

string folderName = Path.GetFileName(Path.GetDirectoryName(@"c:\projects\root\wsdlproj\devlop\beta2\text\GTA.exe"));//Return "text"


답변

text.txt가 디렉토리인지 파일인지 확인하기 위해 파일 시스템으로 이동하는 것 외에는 방법이 없다고 생각했습니다. 간단한 것을 원하면 다음을 사용할 수 있습니다.

s.Substring(s.LastIndexOf(@"\"));


답변

이 경우 얻고 자하는 파일은 strpath 변수에 저장됩니다 :

string strPath = Server.MapPath(Request.ApplicationPath) + "/contents/member/" + strFileName;