Working with DriveInfo and Directory in Windows Application Form

Posted by Xolani under .NET Certifications on 2/24/2011 | Points: 10 | Views : 1510 | Status : [Member] | Replies : 1
Hi

I have created an appication called MyComputer Drives. It purpose is to show all the current drive in myComputer in listBox1. when you select any drive in listbox1, it must show all the information of the selected drive, list all the folder and other document which are maybe out side folders in listBox2. The same applies to lisbox2 when u click the folder or file it should display all the information in different labels and also show a picture of the format(pdf/folder/word.doc).Im having difficuties working with the code that must display info in lisbox2 after I selected drive. I new in Csharp and a beggining programme this code might not meet certain level or be proper format.

public partial class Form1 : Form
{
DriveInfo[] drive;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
listBox1.Text = ((DriveInfo)listBox1.SelectedItem).Name;

string selectD = drive[listBox1.SelectedIndex].Name;
listBox1.SelectedIndex.ToString(selectD);
listBox2.DataSource = selectD;
}
catch
{
listBox1.Text = string.Empty;
}
}

private void button1_Click(object sender, EventArgs e)
{
AddDriver();
}

public void AddDriver()
{
try
{
foreach (DriveInfo drive in dI)
{
listBox1.Items.Add(drive);
}
listBox1.Sorted = true;

}
catch (Exception exp)
{
MessageBox.Show("Error while trying to access drives", exp.Message);
}
}

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{

DriveInfo[] dI = DriveInfo.GetDrives();
if (listBox2_SelectedIndexChanged == true)
try
{
dI = drive[listBox1.SelectedIndex].RootDirectory();
if (listBox2.SelectedIndex == -1) listBox2.SelectedIndex = 0;

foreach ( DriveInfo dri in selectD)
{
//FileInfo info = new FileInfo();
label2.Text = ("Drive {0}"+ dri.Name);
label3.Text = (" File type: "+ dri.DriveType);
label4.Text =("Volume label: "+ dri.VolumeLabel);
label5.Text = ("Total size of drive: "+ dri.TotalSize + "Bytes");
label6.Text = ("Size : "+ dri.TotalFreeSpace);
}

}
else dI = drive[listBox1.SelectedIndex]
//listBox3.Items.Clear();
}
catch
{
//to do
}

}

}
}

It looks awful I know. I will appriciate it if you can help me or advice me on how to go about doing it the right way.
Thank . from Axe




Responses

Posted by: Madhu.b.rokkam on: 2/24/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
We have lot many code blocks written on getting drive info.. plese check that .

Thanks and Regards
Madhu

Xolani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response