C#
(C#) 파일명에서 확장자, 파일명(확장자가 없는) 분리하기
코딩ABC
2023. 5. 24. 16:46
반응형
다음 코드는 파일명에서 확장자를 분리하는 코드입니다.
확장자와 확장자를 제거한 파일명을 출력합니다.
GetExtention() 메서드는 확장명에 점(.)까지 포함하고 있기 때문에 다시 결합할 때 점은 결합할 필요가 없습니다.
string file = Path.GetFileNameWithoutExtension(filename); string ext = Path.GetExtension(filename); |
using System.IO;
private void button2_Click(object sender, EventArgs e)
{
string filename = "abc.exe";
string file = Path.GetFileNameWithoutExtension(filename);
string ext = Path.GetExtension(filename);
listBox1.Items.Add(filename);
listBox1.Items.Add(file);
listBox1.Items.Add(ext);
}
private void button1_Click(object sender, EventArgs e)
{
string path = "c:\\temp\\earth-2.jpg";
string fullpath = Path.GetFullPath(path);
string dir = Path.GetDirectoryName(path);
string file1 = Path.GetFileName(path);
string file2 = Path.GetFileNameWithoutExtension(path);
string ext = Path.GetExtension(path);
listBox1.Items.Add(fullpath);
listBox1.Items.Add(dir);
listBox1.Items.Add(file1);
listBox1.Items.Add(file2);
listBox1.Items.Add(ext);
}
반응형