반응형
C #의 경로 문자열에서 파일 이름 가져 오기
WPF C #에서 프로그래밍합니다. 예를 들어 다음 경로가 있습니다.
C:\Program Files\hello.txt
그리고 " hello " 를 출력하고 싶습니다 .
경로는 데이터베이스에서 추출한 문자열입니다. 현재 다음 방법을 사용하고 있습니다 (경로에서 '\'로 분리 한 다음 '.'로 다시 나눕니다).
string path = "C:\\Program Files\\hello.txt";
string[] pathArr = path.Split('\\');
string[] fileArr = pathArr.Last().Split('.');
string fileName = fileArr.Last().ToString();
작동하지만 더 짧고 똑똑한 솔루션이 있어야한다고 생각합니다. 어떤 생각?
경로 클래스는 훌륭합니다.
시험
fileName = Path.GetFileName (path);
http://msdn.microsoft.com/de-de/library/system.io.path.getfilename.aspx
시험
System.IO.Path.GetFileNameWithoutExtension(path);
데모
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'",
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path, result);
// This code produces output similar to the following:
//
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
// GetFileName('C:\mydir\') returns ''
다음과 같이 Path API를 사용할 수 있습니다 .
var filenNme = Path.GetFileNameWithoutExtension([File Path]);
추가 정보 : Path.GetFileNameWithoutExtension
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
이 시도:
string fileName = Path.GetFileNameWithoutExtension(@"C:\Program Files\hello.txt");
fileName에 "hello"를 반환합니다.
string Location = "C:\\Program Files\\hello.txt";
string FileName = Location.Substring(Location.LastIndexOf('\\') +
1);
이 시도,
string FilePath=@"C:\mydir\myfile.ext";
string Result=Path.GetFileName(FilePath);//With Extension
string Result=Path.GetFileNameWithoutExtension(FilePath);//Without Extension
Namespace: using System.IO;
//use this to get file name dynamically
string filelocation = Properties.Settings.Default.Filelocation;
//use this to get file name statically
//string filelocation = @"D:\FileDirectory\";
string[] filesname = Directory.GetFiles(filelocation); //for multiple files
Your path configuration in App.config file if you are going to get file name dynamically -
<userSettings>
<ConsoleApplication13.Properties.Settings>
<setting name="Filelocation" serializeAs="String">
<value>D:\\DeleteFileTest</value>
</setting>
</ConsoleApplication13.Properties.Settings>
</userSettings>
string filepath = "C:\\Program Files\\example.txt";
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(filepath);
FileInfo fi = new FileInfo(filepath);
Console.WriteLine(fi.Name);
//input to the "fi" is a full path to the file from "filepath"
//This code will return the fileName from the given path
//output
//example.txt
참고 URL : https://stackoverflow.com/questions/6921105/get-file-name-from-a-path-string-in-c-sharp
반응형
'Programing' 카테고리의 다른 글
Java 옵션 -Xmx의 의미는 무엇입니까? (0) | 2020.04.07 |
---|---|
JPG 및 JPEG 이미지 형식 (0) | 2020.04.07 |
ReSharper는 경고 : "일반 유형의 정적 필드" (0) | 2020.04.06 |
프로젝트가 빌드 된 경우에도 Visual Studio에서 오류 표시 (0) | 2020.04.06 |
OS 독립 경로 'META-INF / LICENSE'와 함께 둘 이상의 파일이 발견되었습니다. (0) | 2020.04.06 |