My Explorer In C#
| ||||||||||||||||
IntroductionThis article shows how to get system drives information using Getting StartedFirst we need to gather information on all the drives my computer have access to, and display the name and type of drive in the Collapse //This procedure populate the TreeView with the Drive list
private void PopulateDriveList()
{
TreeNode nodeTreeNode;
int imageIndex = 0;
int selectIndex = 0;
const int Removable = 2;
const int LocalDisk = 3;
const int Network = 4;
const int CD = 5;
//const int RAMDrive = 6;
this.Cursor = Cursors.WaitCursor;
//clear TreeView
tvFolders.Nodes.Clear();
nodeTreeNode = new TreeNode("My Computer",0,0);
tvFolders.Nodes.Add(nodeTreeNode);
//set node collection
TreeNodeCollection nodeCollection = nodeTreeNode.Nodes;
//Get Drive list
ManagementObjectCollection queryCollection = getDrives();
foreach ( ManagementObject mo in queryCollection)
{
switch (int.Parse( mo["DriveType"].ToString()))
{
case Removable: //removable drives
imageIndex = 5;
selectIndex = 5;
break;
case LocalDisk: //Local drives
imageIndex = 6;
selectIndex = 6;
break;
case CD: //CD rom drives
imageIndex = 7;
selectIndex = 7;
break;
case Network: //Network drives
imageIndex = 8;
selectIndex = 8;
break;
default: //defalut to folder
imageIndex = 2;
selectIndex = 3;
break;
}
//create new drive node
nodeTreeNode = new TreeNode(mo["Name"].ToString()
+ "\\" ,imageIndex,selectIndex);
//add new node
nodeCollection.Add(nodeTreeNode);
}
//Init files ListView
InitListView();
this.Cursor = Cursors.Default;
}
protected ManagementObjectCollection getDrives()
{
//get drive collection
ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
return queryCollection;
}
Directories and FilesWhen we click on a drive or a directory in the Collapse protected void PopulateDirectory(TreeNode nodeCurrent,
TreeNodeCollection nodeCurrentCollection)
{
TreeNode nodeDir;
int imageIndex = 2;
int selectIndex = 3;
if (nodeCurrent.SelectedImageIndex != 0)
{ //populate treeview with folders
try
{
if(Directory.Exists(getFullPath(nodeCurrent.FullPath))
== false)
{
MessageBox.Show("Directory or path " +
nodeCurrent.ToString() + " does not exist.");
}
else
{ //populate files
PopulateFiles(nodeCurrent);
string[] stringDirectories =
Directory.GetDirectories(getFullPath
(nodeCurrent.FullPath));
string stringFullPath = "";
string stringPathName = "";
foreach (string stringDir in stringDirectories)
{
stringFullPath = stringDir;
stringPathName = GetPathName(stringFullPath);
//create node for directories
nodeDir = new TreeNode(stringPathName.ToString(),
imageIndex,selectIndex);
nodeCurrentCollection.Add(nodeDir);
}
}
}
catch (IOException e)
{
MessageBox.Show("Error:
Drive not ready or directory does not exist.");
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("Error:
Drive or directory access denided.");
}
catch (Exception e)
{
MessageBox.Show("Error: " + e);
}
}
}
protected string GetPathName(string stringPath)
{
//Get Name of folder
string[] stringSplit = stringPath.Split('\\');
int _maxIndex = stringSplit.Length;
return stringSplit[_maxIndex-1];
}
protected void PopulateFiles(TreeNode nodeCurrent)
{
//Populate listview with files
string[] lvData = new string[4];
//clear list
InitListView();
if (nodeCurrent.SelectedImageIndex != 0)
{
//check path
if(Directory.Exists((string)
getFullPath(nodeCurrent.FullPath)) == false)
{
MessageBox.Show("Directory or path " +
nodeCurrent.ToString() + " does not exist.");
}
else
{
try
{
string[] stringFiles = Directory.GetFiles
(getFullPath(nodeCurrent.FullPath));
string stringFileName = "";
DateTime dtCreateDate, dtModifyDate;
Int64 lFileSize = 0;
//loop throught all files
foreach (string stringFile in stringFiles)
{
stringFileName = stringFile;
FileInfo objFileSize = new
FileInfo(stringFileName);
lFileSize = objFileSize.Length;
//GetCreationTime(stringFileName);
dtCreateDate = objFileSize.CreationTime;
//GetLastWriteTime(stringFileName);
dtModifyDate = objFileSize.LastWriteTime;
//create listview data
lvData[0] = GetPathName(stringFileName);
lvData[1] = formatSize(lFileSize);
//check if file is in local current
//day light saving time
if (TimeZone.CurrentTimeZone.
IsDaylightSavingTime(dtCreateDate) == false)
{
//not in day light saving time adjust time
lvData[2] = formatDate(dtCreateDate.AddHours(1));
}
else
{
//is in day light saving time adjust time
lvData[2] = formatDate(dtCreateDate);
}
//check if file is in local current day
//light saving time
if (TimeZone.CurrentTimeZone.
IsDaylightSavingTime(dtModifyDate) == false)
{
//not in day light saving time adjust time
lvData[3] = formatDate(dtModifyDate.AddHours(1));
}
else{
//not in day light saving time adjust time
lvData[3] = formatDate(dtModifyDate);
}
//Create actual list item
ListViewItem lvItem = new ListViewItem(lvData,0);
lvFiles.Items.Add(lvItem);
}
}
catch (IOException e)
{
MessageBox.Show("Error:
Drive not ready or directory does not exist.");
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("Error:
Drive or directory access denided.");
}
catch (Exception e)
{
MessageBox.Show("Error: " + e);
}
}
}
}
protected string getFullPath(string stringPath)
{
//Get Full path string
stringParse = "";
//remove My Computer from path.
stringParse = stringPath.Replace("My Computer\\", "");
return stringParse;
}
protected string formatDate(DateTime dtDate)
{
//Get date and time in short format
string stringDate = "";
stringDate = dtDate.ToShortDateString().ToString()
+ " " + dtDate.ToShortTimeString().ToString();
return stringDate;
}
protected string formatSize(Int64 lSize)
{
//Format number to KB
string stringSize = "";
NumberFormatInfo myNfi = new NumberFormatInfo();
Int64 lKBSize = 0;
if (lSize < 1024 )
{
if (lSize == 0)
{
//zero byte
stringSize = "0";
}
else
{
//less than 1K but not zero byte
stringSize = "1";
}
}
else
{
//convert to KB
lKBSize = lSize / 1024;
//format number with default format
stringSize = lKBSize.ToString("n",myNfi);
//remove decimal
stringSize = stringSize.Replace(".00", "");
}
return stringSize + " KB";
}
Formating NumbersInstead of writing string parsing functions to format the file size with comma in the thousand and millionth position, I use the protected string formatSize(Int64 lSize)
{
//Format number to KB
string stringSize = "";
NumberFormatInfo myNfi = new NumberFormatInfo();
Int64 lKBSize = 0;
if (lSize < 1024 )
{
:
:
}
else
{
//convert to KB
lKBSize = lSize / 1024;
//format number with default format
stringSize = lKBSize.ToString("n",myNfi);
//remove decimal
stringSize = stringSize.Replace(".00", "");
}
return stringSize + " KB";
}
Day Light Saving TimeAt first glance, it looks like FileInfo objFileSize = new FileInfo(stringFileName) will give you all the correct information about the file to display on the screen. But it turns out that the file date time may not be correct due to Day Light Saving Time settings on your machine and how the file was saved. The files that does not have the Day Light Saving Time set to true will have the create, modify, and access time off by one hour. We need to check for this condition using the function Collapse
protected void PopulateFiles(TreeNode nodeCurrent)
{
//Populate listview with files
string[] lvData = new string[4];
//clear list
InitListView();
if (nodeCurrent.SelectedImageIndex != 0)
{
//check path
if(Directory.Exists((string)
getFullPath(nodeCurrent.FullPath)) == false)
{
MessageBox.Show("Directory or path " +
nodeCurrent.ToString() + " does not exist.");
}
else
{
try
{
string[] stringFiles = Directory.
GetFiles(getFullPath(nodeCurrent.FullPath));
string stringFileName = "";
DateTime dtCreateDate, dtModifyDate;
Int64 lFileSize = 0;
//loop throught all files
foreach (string stringFile in stringFiles)
{
stringFileName = stringFile;
FileInfo objFileSize = new FileInfo(stringFileName);
lFileSize = objFileSize.Length;
//GetCreationTime(stringFileName);
dtCreateDate = objFileSize.CreationTime;
//GetLastWriteTime(stringFileName);
dtModifyDate = objFileSize.LastWriteTime;
//create listview data
lvData[0] = GetPathName(stringFileName);
lvData[1] = formatSize(lFileSize);
//check if file is in local current
//day light saving time
if (TimeZone.CurrentTimeZone.
IsDaylightSavingTime(dtCreateDate) == false)
{
//not in day light saving time adjust time
lvData[2] = formatDate(dtCreateDate.AddHours(1));
}
else
{
//is in day light saving time adjust time
lvData[2] = formatDate(dtCreateDate);
}
//check if file is in local current
//day light saving time
if (TimeZone.CurrentTimeZone.
IsDaylightSavingTime(dtModifyDate) == false)
{
//not in day light saving time adjust time
lvData[3] = formatDate(dtModifyDate.AddHours(1));
}
else {
//not in day light saving time adjust time
lvData[3] = formatDate(dtModifyDate);
}
//Create actual list item
ListViewItem lvItem = new ListViewItem(lvData,0);
lvFiles.Items.Add(lvItem);
}
}
catch (IOException e)
{
MessageBox.Show("Error:
Drive not ready or directory does not exist.");
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show("Error:
Drive or directory access denied.");
}
catch (Exception e)
{
MessageBox.Show("Error: " + e);
}
}
}
}
|