반응형
파일 이름이 포함 된 경로에서 파일 이름없이 전체 경로 가져 오기
내장이 무엇인가 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);
그림과 같이 '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));
}
}
}
반응형
'Programing' 카테고리의 다른 글
Perl에서 나와 우리의 차이점은 무엇입니까? (0) | 2020.05.19 |
---|---|
동적으로 삽입 된 iframe의 jQuery .ready (0) | 2020.05.19 |
매크로에서 익명 클래스의 메소드를 사용하여 구조 유형 가져 오기 (0) | 2020.05.18 |
Android에서 가로 ListView를 만들려면 어떻게해야합니까? (0) | 2020.05.18 |
iOS 8은 "minimal-ui"뷰포트 속성을 제거했습니다. 다른 "소프트 풀 스크린"솔루션이 있습니까? (0) | 2020.05.18 |