본문 바로가기
프로그래밍/C#, WPF, .Net

[C#, WPF] 파일 복사, 삭제, 크기, 확장명, 삭제, 시간 정보 얻어 오는 방법 (FIleInfo, System.IO)

by GhostWeb 2021. 5. 19.
반응형

- 코드

using System.IO;

FileInfo fileInfo = new FileInfo(@"D:\ubuntu-20.04.2-live-server-amd64.iso");

if (fileInfo.Exists)
{
    Debug.WriteLine("File FullName : " + fileInfo.FullName);
    Debug.WriteLine("File Name : " + fileInfo.Name);
    Debug.WriteLine("File Name : " + fileInfo.Extension);    
    Debug.WriteLine("File Length : " + fileInfo.Length + " byte");

    Debug.WriteLine("File CreationTime : " + fileInfo.CreationTime.ToString());
    Debug.WriteLine("File LastWriteTime : " + fileInfo.LastWriteTime.ToString());

    string copyFilePath = @"D:\ubuntu-20.04.2-live-server-amd64_Copy.iso";
    fileInfo.CopyTo(copyFilePath);
    //fileInfo.CopyTo(copyFilePath, true);

    fileInfo.Delete();
}

 

- 실행 결과

File FullName : D:\ubuntu-20.04.2-live-server-amd64.iso
File Name : ubuntu-20.04.2-live-server-amd64.iso
File Name : .iso
File Length : 1215168512 byte
File CreationTime : 2021-05-19 오후 5:16:41
File LastWriteTime : 2021-04-12 오후 7:51:25

 

- 파일 크기

public long Length { get; }

 

- 파일 전체 경로, 파일 이름, 파일 확장명

// 파일 전체 경로
public virtual string FullName { get; }

// 파일 이름
public abstract string Name { get; }

// 파일의 확장명
public string Extension { get; }

 

- 파일 복사

// 파일 복사
public System.IO.FileInfo CopyTo (string destFileName, bool overwrite);

bool overwrite 
> true : 덮어 쓰기
> false : 덮어 쓰지 않음

 

- 파일 삭제

public override void Delete();
반응형