Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 파일 크기를 어떻게 얻습니까?

<시간/>

FileInfo 클래스는 C#에서 파일 및 해당 작업을 처리하는 데 사용됩니다.

파일 생성, 삭제 및 읽기에 사용되는 속성 및 메서드를 제공합니다. StreamWriter 클래스를 사용하여 파일에 데이터를 씁니다. System.IO 네임스페이스의 일부입니다.

Directory 속성은 파일의 부모 디렉터리를 나타내는 개체를 검색합니다.

DirectoryName 속성은 파일 상위 디렉터리의 전체 경로를 검색합니다.

Exists 속성은 작업하기 전에 파일이 있는지 확인합니다.

IsReadOnly 속성은 파일을 수정할 수 있는지 여부를 지정하는 값을 검색하거나 설정합니다.

Length는 파일의 크기를 검색합니다.

이름은 파일 이름을 검색합니다.

예시

class Program{
   public static void Main(){
      var path = @"C:\Users\Koushik\Desktop\Questions\ConsoleApp\Data.csv";
      long length = new System.IO.FileInfo(path).Length;
      System.Console.WriteLine(length);
   }
}

출력

12

예시

class Program{
   public static void Main(){
      var path = @"C:\Users\Koushik\Desktop\Questions\ConsoleApp";
      DirectoryInfo di = new DirectoryInfo(path);
      FileInfo[] fiArr = di.GetFiles();
      Console.WriteLine("The directory {0} contains the following files:", di.Name);
      foreach (FileInfo f in fiArr)
         Console.WriteLine("The size of {0} is {1} bytes.", f.Name, f.Length);
   }
}

출력

The directory ConsoleApp contains the following files:
The size of ConsoleApp.csproj is 333 bytes.
The size of Data.csv is 12 bytes.
The size of Program.cs is 788 bytes.