Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# ดูข้อมูลไฟล์

ใช้ FileInfo.Attributes Property ดึง FileAttributes (Flags Enum) ที่บอกสถานะไฟล์: ReadOnly, Hidden, System, Directory, Archive, Device, Normal, Temporary, SparseFile, ReparsePoint, Compressed, Offline, NotContentIndexed, Encrypted, IntegrityStream, NoScrubData

ตัวอย่างโค้ด

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        using (StreamWriter sw = new StreamWriter("hello.txt"))
        {
            sw.WriteLine("This is demo text!");
        }
        
        FileInfo info = new FileInfo("hello.txt");
        FileAttributes attr = info.Attributes;
        Console.WriteLine(attr);
    }
}

ผลลัพธ์

Normal

ตรวจสอบแฟล็กเฉพาะ: (attr & FileAttributes.Hidden) == FileAttributes.Hidden