HOWTO · Csharp

C#에서 파일 삭제

File.Delete(path) 함수를 사용하여 C#에서 지정된 경로 내의 파일을 삭제할 수 있습니다.

이 페이지의 내용

이 자습서에서는 C#의 특정 경로에서 파일을 삭제하는 방법을 소개합니다.

C#에서File.Delete(path)함수를 사용하여 파일 삭제

File.Delete(path)함수를 사용하여 C#의path경로 안에있는 파일. 다음 코드 예제는 C#에서File.Delete()함수를 사용하여 지정된 경로에서 파일을 삭제하는 방법을 보여줍니다.

using System;
using System.IO;

namespace check_whether_a_file_exists {
  class Program {
    static void Main(string[] args) {
      string path = "C:\\filefolder\\file.txt";
      bool result = File.Exists(path);
      if (result == true) {
        Console.WriteLine("File Found");
        File.Delete(path);
        Console.WriteLine("File Deleted Successfully");
      } else {
        Console.WriteLine("File Not Found");
      }
    }
  }
}

출력:

File Found
File Deleted Successfully

C#의File.Delete()함수를 사용하여C:\\filefolder\\file.txt경로에있는 파일을 삭제했습니다. 우리 프로그램은 먼저File.Exists()함수를 사용하여path내에 파일이 존재하는지 여부를 확인합니다. 파일이 존재하면 프로그램은File.Delete()함수를 사용하여 파일을 삭제합니다. 파일이 없으면 프로그램은File Not Found을 표시합니다.