C#
(C#) 파일명 확장자만 가져오기
코딩ABC
2023. 4. 20. 06:20
반응형
C# 코드: 파일명에서 확장자만 가져오는 코드입니다.
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = openFileDialog1.FileName; // full path
listBox1.Items.Add(filename);
// 경로를 뺀 파일명
listBox1.Items.Add(openFileDialog1.SafeFileName);
listBox1.Items.Add(Path.GetFileName(filename)); // 경로를 뺀 파일명
// 확장명만 가져오기
listBox1.Items.Add(Path.GetExtension(filename));
// 확장명을 뺀 파일명가 가져오기
listBox1.Items.Add(Path.GetFileNameWithoutExtension(filename));
}
}
반응형