C#에서 파일 삭제

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을 표시합니다.

튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

관련 문장 - Csharp File