The class to process the file and directory mainly include Directory, DirectoryInfo, File, FileInfo
Directory : create, move, rename, delete directory with static method . if you will use a directory object many times, you’d better use DirectoryInfo class to create a DreictoryInfo object
DirectoryInfo: be similar with Directory, only differece is that it need to create a object
File: create, move, rename, delete file with static method
FileInfo: be similar with File, only difference is that it uses to create, move, rename, delete file by Creating FileInfo Object
Samples:
protected DataTable CreateDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add("filename", typeof(string));
dt.Columns.Add("size", typeof(int));
dt.Columns.Add("type", typeof(int));
dt.Columns.Add("modified", typeof(DateTime));
//dt.DefaultView.Sort = "type," + dgFiles.GetOrderBy();
return dt;
}
DirectoryInfo dirInfo = new DirectoryInfo(“c:\”);
FileInfo[] info = dirInfo.GetFiles();
DirectoryInfo[] dirs = dirInfo.GetDirectories();
DataTable dt = CreateDataSource();// undefined!!!
DataRow dr;
foreach (DirectoryInfo dir in dirs)
{
dr = dt.NewRow();
dr["filename"] = dir.Name;
dr["size"] = "0";
dr["type"] = "0";
dt.Rows.Add(dr);
}
foreach (FileInfo file in info)
{
dr = dt.NewRow();
dr["filename"] = file.Name;
dr["size"] = (int)file.Length / 1024;
dr["type"] = "1";
dr["modified"] = file.LastWriteTime;
dt.Rows.Add(dr);
}