Tuesday, 25 December 2012 13:56

c# directories and files

Written by 
Rate this item
(0 votes)

in windows invironment,the file system is splited into directories
and files.files holds the actual data,but directories are
storage for logical listing of files in the system.to deal with
files and folders(directories),CLR uses System.IO namespace.
the classes used are File,FileInfo,DirectoryInfo,Directory.

the File and FileInfo classes are almost same in functionality.
the difference is that the File class have static methods for
handling files but FileInfo class have instance methods for
dealing with the files.

both File and FileInfo classes use 'Copy' to copy file to a
destination location.they use 'Create' to create files.they
use 'Delete' for deleting a file.they use Exists to check
file existance.but File class uses 'Exists' as method and
FileInfo class uses 'Exists' as property.they use 'Move' for
moving the file.they both use 'Open' for opening FileStream to
file.

FileInfo class includes a constructor which takes one parameter
of a string(contains the path of the file).the path to file may
be totally qualified path or relative.when making FileInfo object
the file is not needed to be existed.

following shows a example to construct a FileInfo obj-
FileInfo file1 = new FileInfo("d:\\tempo\\csharp.txt");//totally
FileInfo file2 = new FileInfo("\\datab\\Data.xml");//Relational
FileInfo file3 = new FileInfo(@"\datab\myDataFile.xml");
//Using a verbatim string


some valuable properties of the FileInfo class are follows-
1.Attributes
2.DirectoryName
3.CreationTime
4.Exists
5.FullName
6.Name
7.Length

because the File class includes static methods,you may conduct
many file functions besides making an instance of file as shown-

//for Copying a file
File.Copy(@"c:\tempodir\csharp.txt",@"e:\tempodir\csharp.txt");
//for creating file
File.Create(@"d:\tempodir\newdocument.txt");

Read 4023 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.