Retrieve Information About a File, Directory, or Drive - CSharp File IO

CSharp examples for File IO:File

Description

Retrieve Information About a File, Directory, or Drive

Demo Code


using System;// w  ww . j a v  a  2 s. c  o m
using System.IO;

    static class MainClass
    {
        static void Main(string[] args)
        {
            FileInfo file = new FileInfo("Main.cs");

            Console.WriteLine("Checking file: " + file.Name);
            Console.WriteLine("File exists: " + file.Exists.ToString());

            if (file.Exists)
            {
                Console.Write("File created: ");
                Console.WriteLine(file.CreationTime.ToString());
                Console.Write("File last updated: ");
                Console.WriteLine(file.LastWriteTime.ToString());
                Console.Write("File last accessed: ");
                Console.WriteLine(file.LastAccessTime.ToString());
                Console.Write("File size (bytes): ");
                Console.WriteLine(file.Length.ToString());
                Console.Write("File attribute list: ");
                Console.WriteLine(file.Attributes.ToString());
            }
            Console.WriteLine();

            DirectoryInfo dir = file.Directory;

            Console.WriteLine("Checking directory: " + dir.Name);
            Console.WriteLine("In directory: " + dir.Parent.Name);
            Console.Write("Directory exists: ");
            Console.WriteLine(dir.Exists.ToString());

            if (dir.Exists)
            {
                Console.Write("Directory created: ");
                Console.WriteLine(dir.CreationTime.ToString());
                Console.Write("Directory last updated: ");
                Console.WriteLine(dir.LastWriteTime.ToString());
                Console.Write("Directory last accessed: ");
                Console.WriteLine(dir.LastAccessTime.ToString());
                Console.Write("Directory attribute list: ");
                Console.WriteLine(dir.Attributes.ToString());
                Console.WriteLine("Directory contains: " +
                  dir.GetFiles().Length.ToString() + " files");
            }
            Console.WriteLine();

            // Display drive information.
            DriveInfo drv = new DriveInfo(file.FullName);

            Console.Write("Drive: ");
            Console.WriteLine(drv.Name);

            if (drv.IsReady)
            {
                Console.Write("Drive type: ");
                Console.WriteLine(drv.DriveType.ToString());
                Console.Write("Drive format: ");
                Console.WriteLine(drv.DriveFormat.ToString());
                Console.Write("Drive free space: ");
                Console.WriteLine(drv.AvailableFreeSpace.ToString());
            }

        }
    }

Result


Related Tutorials