Exploring Drives,directories and Files
Create a windows form in C#, with a combo box, treeview, listbox and a picture box.
private void exploreEg_Load(object sender, EventArgs e)
{
foreach (DriveInfo d in DriveInfo.GetDrives())
comboBox1.Items.Add(d.Name);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DriveInfo dr = new DriveInfo(comboBox1.SelectedItem.ToString());
if (dr.IsReady)
{
treeView1.Nodes.Clear();
DirectoryInfo di = new DirectoryInfo(dr.Name);
listBox1.Items.Clear();
foreach (FileInfo fi in di.GetFiles())
{
if (fi.Extension.ToUpper().Trim() == ".JPG")
listBox1.Items.Add(fi.FullName);
}
foreach (DirectoryInfo d in di.GetDirectories())
{
treeView1.Nodes.Add(d.FullName);
}
}
else
MessageBox.Show("Drive is not ready");
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
DirectoryInfo di = new DirectoryInfo(treeView1.SelectedNode.Text);
if (di.Exists)
{
listBox1.Items.Clear();
foreach (DirectoryInfo d in di.GetDirectories())
{
treeView1.SelectedNode.Nodes.Add(d.FullName);
}
foreach (FileInfo fi in di.GetFiles())
{
if (fi.Extension.ToUpper().Trim() == ".JPG")
listBox1.Items.Add(fi.FullName);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FileInfo fi = new FileInfo(listBox1.SelectedItem.ToString());
label1.Text ="Created Time "+ fi.CreationTime.ToString();
label2.Text ="Directory "+ fi.DirectoryName;
label3.Text = "Extension "+fi.Extension;
pictureBox1.Image = Image.FromFile(listBox1.SelectedItem.ToString());
}
Happy Programming !!!!!!!!!!!!!!!!
0 comments:
Post a Comment