확장자명 추가하기

2023. 8. 23. 00:38카테고리 없음

DEM_AA_01.hdr   ---->  DEM_AA_01.hdr

 

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string folderPath = @"C:\DTEDD\hdr"; // 폴더 경로를 변경하세요.
        string newExtension = ".hdr"; // 추가할 확장자를 설정하세요.

        // 폴더 내의 모든 파일 가져오기
        string[] files = Directory.GetFiles(folderPath);

        foreach (string filePath in files)
        {
            AddExtensionToFileName(filePath, newExtension);
        }

        Console.WriteLine("작업 완료.");
    }

    static void AddExtensionToFileName(string filePath, string extension)
    {
        try
        {
            // 새로운 파일 경로 생성 (확장자 추가)
            string newFilePath = filePath + extension;

            // 파일 이름 변경
            File.Move(filePath, newFilePath);

            Console.WriteLine($"파일 이름 변경: {filePath} -> {newFilePath}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"오류: {ex.Message}");
        }
    }
}