다음을 통해 공유


File.Delete 메서드

지정된 파일을 삭제합니다. 지정된 파일이 없어도 예외가 throw되지 않습니다.

네임스페이스: System.IO
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Shared Sub Delete ( _
    path As String _
)
‘사용 방법
Dim path As String

File.Delete(path)
public static void Delete (
    string path
)
public:
static void Delete (
    String^ path
)
public static void Delete (
    String path
)
public static function Delete (
    path : String
)

매개 변수

  • path
    삭제할 파일의 이름입니다.

예외

예외 형식 조건

ArgumentException

path가 길이가 0인 문자열이거나, 공백만 포함하거나 또는 InvalidPathChars로 정의된 하나 이상의 잘못된 문자를 포함하는 경우

ArgumentNullException

path가 Null 참조(Visual Basic의 경우 Nothing)인 경우

DirectoryNotFoundException

지정된 경로가 잘못된 경우(예: 매핑되지 않은 드라이브의 경로를 지정한 경우)

IOException

지정된 파일이 사용되고 있는 경우

NotSupportedException

path의 형식이 잘못된 경우

PathTooLongException

지정된 경로 또는 파일 이름이 시스템에 정의된 최대 길이를 초과하는 경우. 예를 들어, Windows 기반 플랫폼에서는 경로에 248자 미만의 문자를 사용해야 하며 파일 이름에는 260자 미만의 문자를 사용해야 합니다.

UnauthorizedAccessException

호출자에게 필요한 권한이 없는 경우

- 또는 -

path가 디렉터리인 경우

- 또는 -

path에서 읽기 전용 파일을 지정한 경우

설명

path 매개 변수에는 상대 경로나 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리에 상대적으로 해석됩니다. 현재 작업 디렉터리를 얻는 방법에 대해서는 GetCurrentDirectory를 참조하십시오.

이 메서드의 사용에 대한 예제는 예제 단원을 참조하십시오. 다음 표에서는 일반적인 예 또는 관련된 I/O 작업의 예를 보여 줍니다.

수행 작업

참조 항목

텍스트 파일을 만듭니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에 씁니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에서 읽습니다.

방법: 파일의 텍스트 읽기

파일에 텍스트를 추가합니다.

방법: 로그 파일 열기 및 추가

File.AppendText

FileInfo.AppendText

파일을 복사합니다.

File.Copy

FileInfo.CopyTo

파일 이름을 바꾸거나 이동합니다.

File.Move

FileInfo.MoveTo

디렉터리를 삭제합니다.

Directory.Delete

DirectoryInfo.Delete

이진 파일에서 읽습니다.

방법: 새로 만든 데이터 파일 읽기 및 쓰기

이진 파일에 씁니다.

방법: 새로 만든 데이터 파일 읽기 및 쓰기

디렉터리를 만듭니다.

CreateDirectory

Directory

디렉터리의 파일을 참조하십시오.

Name

파일 특성을 설정합니다.

SetAttributes

Windows NT 4.0 플랫폼 참고: Delete는 일반 I/O용으로 열린 파일이나 메모리에 매핑된 파일은 삭제하지 않습니다.

예제

다음 예제에서는 지정된 경로에서 파일을 삭제합니다.

Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Try
            Dim sw As StreamWriter = File.CreateText(path)
            sw.Close()
            Dim path2 As String = path + "temp"

            ' Ensure that the target does not exist.
            File.Delete(path2)

            ' Copy the file.
            File.Copy(path, path2)
            Console.WriteLine("{0} was copied to {1}.", path, path2)

            ' Delete the newly created file.
            File.Delete(path2)
            Console.WriteLine("{0} was successfully deleted.", path2)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        try 
        {
            using (StreamWriter sw = File.CreateText(path)) {}
            string path2 = path + "temp";

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            File.Delete(path2);
            Console.WriteLine("{0} was successfully deleted.", path2);
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      StreamWriter^ sw = File::CreateText( path );
      if ( sw )
            delete (IDisposable^)sw;

      String^ path2 = String::Concat( path, "temp" );
      
      // Ensure that the target does not exist.
      File::Delete( path2 );
      
      // Copy the file.
      File::Copy( path, path2 );
      Console::WriteLine( "{0} was copied to {1}.", path, path2 );
      
      // Delete the newly created file.
      File::Delete( path2 );
      Console::WriteLine( "{0} was successfully deleted.", path2 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";

        try {    
            StreamWriter sw = File.CreateText(path);
            try {
            }
            finally {
                sw.Dispose();
            }
            String path2 = path + "temp";

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            File.Delete(path2);
            Console.WriteLine("{0} was successfully deleted.", path2);
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test

.NET Framework 보안

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

File 클래스
File 멤버
System.IO 네임스페이스

기타 리소스

파일 및 스트림 I/O
방법: 파일의 텍스트 읽기
방법: 파일에 텍스트 쓰기