Yes, we need to know how to use these classes, and what the relationship between them.for all classes related to Input and Output classes defined in namespace is called using System.IO;
using System.IO
Directory,File,FileSystemInfo,FileInfo,DirectroyInfo,Stream,
FileStream, ...ect.
using System.IO
Directory,File,FileSystemInfo,FileInfo,DirectroyInfo,Stream,
FileStream, ...ect.
What is the Difference between Directory and DirectoryInfo Classes?
Yes,Both DirectoryInfo and Directory has same methods and properties.
How to choose which is best one.? Both are good types. We have choose as per our need of the development requirement
Ex:
But when I go for Static class(Directory) or Instatnce class(DirectoryInfo).
Static Class : To perform simple operations like Directory/file is existed or not, creating..ect.
How to use DirectoryInfo type?
DirectoryInfo dirinfo = new DirectoryInfo(@"E:mani");
dirinfo.Create();
DirectoryInfo[] dirstr = dirinfo.GetDirectories();
foreach (DirectoryInfo item in dirstr)
{
Console.Write(item);
Console.Write("\n");
}
Same example we can perform implement using Static Class Directory
string strpath = @"E:\mani";
Directory.CreateDirectory(strpath); // equal to dirinfo.Create()
string[] strdirectires = Directory.GetDirectories(strpath);
foreach (string item in strdirectires)
{
Console.Write(item);
Console.WriteLine("\n");
}
But how to use them is for simple operations like directory is exist or not I want to know then choose static class---Directory class. Why because I don’t require now any information about that directoy.
Ex:
string strdir=@"E:\a";
if (Directory.Exists(strdir))
{
Console.WriteLine("yes directory is existed");
}
else
{
Console.WriteLine("no directory is no existed");
}
Instead of unneccsseryly creating object like below
DirectoryInfo dirinfo = new DirectoryInfo(@"E:\a");
if (dirinfo.Exists)
{
Console.WriteLine("yes existed sub directories");
}
else
{
Console.WriteLine("no directory is existed");
}
But some titmes I need to know about that particular directory then go for DirectoryInfo.
a diriectory contains b,c,d directories so output is yes directory is existed
But now I want know b or c or d subdirectory conatains any subdirectories.
Then go for DirectoryInfo type.
Static class –Directory require more parameters then DirectoryInfo type.
Like
Directory.MoveTo(source,destination);
Come to DirectoryInfo requries one parameter that is Destination only
Why because we are working with instance of directoryInfo which is already instanated with the some path
Ex:
DirectoryInfo dirinfo = new DirectoryInfo(@"E:\a");
Means dirinfo has source it self. So we requrie only destination so one parameter is enough.
Note: what are the things we can do with the Directory static class. All are possible with DirectoryInfo also. But unnecessarly creating object is not good.
The same things what we discuss applicable to File and FileInfo types.
Why because they are derived from FileSystemInfo abstract class.
No comments:
Post a Comment