Breaking News

Sunday 29 September 2013

File I/O Using Windows File System C#....

C# - Windows File System:

C# allows you to work with directories and files using various directory and file related classes like , the DirectoryInfo class and the FileInfo class.

The DirectoryInfo Class:

The DirectoryInfo class is derived from the FileSystemInfo class. it have various method for creating , moving , and browsing , through directories and subdirectories. this class can not be inherited.

some commonly used properties of the DirectoryInfo class:



  1. Attributes  >> Gets the attributes for the current file or directory.
  2. CreationTime >> Gets the creation time of current file or directory.
  3. Exists >> gets a Boolean value indicating whether the directory exists.
  4. Extension >> Gets the string representing the file extension.
  5. FullName >> Gets the full path of the directory or file.
  6. LastAccessTime >> Gets the time the current file or directory was last accessed.
  7. Name >> Gets the name of this DirectoryInfo instance.

Some commonly used methods of the DirectoryInfo class:

  1. Create()  >> Creates a directory.
  2. CreateSubDirectory(string) >> Create a subdirectory  and subdirectories on the specified path can be relative to this instance of the directory class.
  3. Delete() >> Delete directory info if its empty.
  4. GetDirectories() >> return the subdirectories of the current directories.
  5. GetFiles() >> return a file list from the current directory.
 
The FIleInfo Class:

The FileInfo Class is derived from the FileSystemInfo class. it has properties and instance method  for creating , copying , moving , and opening of files , and helps in the creation of FileStream object. this class cannot be inherit.

some commonly used properties of the FileInfo class:


  • Attributes  >> Gets the attributes for the current file.
  • CreationTime >> Gets the creation time of current file.
  • Exists >> gets a Boolean value indicating whether the file exists.
  • Extension >> Gets the string representing the file extension.
  • FullName >> Gets the full path of the file.
  • LastAccessTime >> Gets the time the current file was last accessed.
  • Name >> Gets the name of this file instance.
  • Directory >> Gets an instance of an directory which the file belongs.
  • LastWriteTime >> gets the time of the last written activity of the file.
  • length >> Gets the size , in bytes , of the current files.
  • Name >> Gets the name of the file.

  • some commonly used method of FileInfo class:

    1. AppendText()  >> Create a StreamWriter that appends text to the file represented by this instance of the FileInfo.
    2. Create()  >> create a file.
    3. Delete()  >> deletes a file permanently.
    4. MoveTo(string destFilename) >> moves a specific file to the new location , providing the option to specify a new file name.
    5. Open(FileMode mode, FileAccess access, FileShare share)  >> opens a file in specified mode with read , write or read/write access and the specified sharing option.
    6. OpenRead()  >> creates a read only FileStream.
    7. OpenWrite()  >> creates a write only FileStream.

    the following example demonstrate the use of DirectoryInfo class :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace directoryinfo_class.cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                DirectoryInfo mydirinfo = new DirectoryInfo("C:");
                Console.WriteLine("full name of the directory is :"+ mydirinfo.FullName);
                Console.WriteLine("the directory was last accessed on :"+ mydirinfo.LastAccessTime.ToString());
                Console.ReadKey();
            }
        }
    }

    output:



    the following example demonstrate the use of FileInfo class :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace fileinfo_classes.cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                // creating an instance of directoryinfo class
                DirectoryInfo mydirinfo = new DirectoryInfo("C:");
                // set all the files in the directory and print their name and size
                FileInfo[] fileindir = mydirinfo.GetFiles();
                foreach (FileInfo file in fileindir)
                {
                        Console.WriteLine("file name : " + file.Name + " extension :" + file.Extension + " size : " + file.Length + "bytes");
                }
                Console.ReadKey();
            }
        }
    }


    output :


    reference link :


    Read more ...
    © 2013 Post by repter_x