파일 이름이 포함 된 경로에서 파일 이름없이 전체 경로 가져 오기 “c : \ webserver

내장이 무엇인가 System.IO.Path가 나에게 단지 파일 경로를 제공?

예를 들어 string

@ “c : \ webserver \ public \ myCompany \ configs \ promo.xml”,

나에게 줄 BCL 방법이 있습니까?

“c : \ webserver \ public \ myCompany \ configs \”?



답변

Path.GetDirectoryName()…하지만 경로에 전달 된 경로에 파일 이름이 포함되어 있음을 알아야합니다. 파일 이름이든 디렉토리 이름이든 실제로 경로에서 최종 비트를 제거합니다 (실제로 어떤 것을 알지 못함).

테스트 File.Exists()및 / 또는 Directory.Exists()경로를 먼저 확인하여 전화해야하는지 확인할 수 있습니다.Path.GetDirectoryName


답변

Console.WriteLine(Path.GetDirectoryName(@"C:\hello\my\dear\world.hm")); 


답변

Path.GetDirectoryName()디렉토리 이름을 반환하므로 원하는대로 (역 후진 문자 사용) 호출 할 수 Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar있습니다.


답변

    string fileAndPath = @"c:\webserver\public\myCompany\configs\promo.xml";

    string currentDirectory = Path.GetDirectoryName(fileAndPath);

    string fullPathOnly = Path.GetFullPath(currentDirectory);

currentDirectory : c : \ webserver \ public \ myCompany \ configs

fullPathOnly : c : \ webserver \ public \ myCompany \ configs


답변

그림과 같이 ‘GetParent ()’를 사용하면 잘 작동합니다. 필요에 따라 오류 검사를 추가하십시오.

var fn = openFileDialogSapTable.FileName;
var currentPath = Path.GetFullPath( fn );
currentPath = Directory.GetParent(currentPath).FullName;


답변

나는 이것을 사용했고 잘 작동한다 :

string[] filePaths = Directory.GetFiles(Path.GetDirectoryName(dialog.FileName));

foreach (string file in filePaths)
{
    if (comboBox1.SelectedItem.ToString() == "")
    {
        if (file.Contains("c"))
        {
            comboBox2.Items.Add(Path.GetFileName(file));
        }
    }
}


답변