파일을 새로 저장하거나 할 때 해당 폴더(디렉토리)가 존재 하지 않으면 에러 발생하니,
해당 폴더가 있는지 확인하고 생성하는 코드 샘플
private static void CheckMakeDir(string filePath)
{
string dir = new FileInfo(filePath).DirectoryName;
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
}
저장한 파일이 잘 저장됐는지 확인을 위해 사이즈 체크하는 코드 샘플
string url = "http://urlurl/test.txt";
string filepath = "c:/test.txt";
Console.WriteLine($"Let's Download {url}");
using (var client = new WebClient())
{
client.DownloadFile(url, filepath);
if(new FileInfo(filepath).Length == 0)
{
throw new Exception($"{filepath} size is zero(0). file download failed.");
}
}
FileInfo에는 좋은게 많아
인터페이스만 봐도 어떻게 사용하는진 알테니
뭐가 있는지 정도는 알아두자
namespace System.IO
{
public sealed class FileInfo : FileSystemInfo
{
public FileInfo(string fileName);
public bool IsReadOnly { get; set; }
public override bool Exists { get; }
public string DirectoryName { get; }
public DirectoryInfo Directory { get; }
public long Length { get; }
public override string Name { get; }
public StreamWriter AppendText();
public FileInfo CopyTo(string destFileName);
public FileInfo CopyTo(string destFileName, bool overwrite);
public FileStream Create();
public StreamWriter CreateText();
public void Decrypt();
public override void Delete();
public void Encrypt();
public void MoveTo(string destFileName);
public FileStream Open(FileMode mode, FileAccess access, FileShare share);
public FileStream Open(FileMode mode, FileAccess access);
public FileStream Open(FileMode mode);
public FileStream OpenRead();
public StreamReader OpenText();
public FileStream OpenWrite();
public FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
public FileInfo Replace(string destinationFileName, string destinationBackupFileName);
public override string ToString();
}
}
- 파일 사이즈
- 생성 시간
- 수정 시간
- 접근 시간
- 읽기전용 파일 여부
- 폴더(디렉토리) 이름
- 파일 이름(url도 된다. http://example.com/test.txt -> test.txt)
- 풀 패스(디렉터리 + 파일 이름)
- ...
c#조으다
'new > new dev' 카테고리의 다른 글
c# 특수문자 제거 (정규식 말고) (0) | 2020.01.29 |
---|---|
룰렛 만들어 볼까? (0) | 2020.01.29 |
쉴 스크립트 sample (0) | 2020.01.03 |
gitlab with docker (0) | 2017.06.07 |
docker + redmine, docker + gitlab (0) | 2017.03.20 |