File and FileInfo

FileInfo offers most of the File's static methods in instance form.

FileInfo also add some additional properties such as Extension, Length, IsReadOnly, and Directory.

For example:


using System;
using System.IO;
using System.Linq;
using System.Text;

class Program
{
    static void Main()
    {
        FileInfo fi = new FileInfo(@"c:\g\customer.xml"); 
        Console.WriteLine(fi.Exists);  // false

        using (TextWriter w = fi.CreateText())
            w.Write("Some text");

        Console.WriteLine(fi.Exists); 
        fi.Refresh();
        Console.WriteLine(fi.Exists); 

        Console.WriteLine(fi.Name); 
        Console.WriteLine (fi.FullName);  
        Console.WriteLine (fi.DirectoryName);  
        Console.WriteLine (fi.Directory.Name); 
        Console.WriteLine (fi.Extension);  
        Console.WriteLine (fi.Length);  

        fi.Encrypt();
        fi.Attributes ^= FileAttributes.Hidden; // (Toggle hidden flag)
        fi.IsReadOnly = true;

        Console.WriteLine(fi.Attributes); // ReadOnly,Archive,Hidden,Encrypted
        Console.WriteLine(fi.CreationTime);
        fi.MoveTo(@"c:\temp\FileInfoX.txt"); 
        DirectoryInfo di = fi.Directory;
        Console.WriteLine(di.Name); 
        Console.WriteLine (di.FullName);  
        Console.WriteLine (di.Parent.FullName);   
        di.CreateSubdirectory ("SubFolder");
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.