Thursday, December 16, 2010

Retrieving File ,Directory and Drive Informations

using System.IO;

private void btnFileInfo_Click(object sender, EventArgs e)
{

FileInfo f = new FileInfo(@"c:\sunil.txt");

if (f.Exists)
{
MessageBox.Show("Full Name " + f.FullName);
MessageBox.Show(" Name " + f.Name);
MessageBox.Show("Extension " + f.Extension);

MessageBox.Show("Size " + f.Length);
}
}

private void btnDirectoryInfo_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
DriveInfo d = new DriveInfo("d:\\");
MessageBox.Show("Free space "+d.TotalFreeSpace.ToString());
MessageBox.Show(d.Name.ToString());

//DirectoryInfo di = new DirectoryInfo("d:\\");
DirectoryInfo di = new DirectoryInfo(d.Name);

foreach (FileInfo fi in di.GetFiles())
{
listBox1.Items.Add(fi.Name);

}

foreach (DirectoryInfo dr in di.GetDirectories())
{
listBox1.Items.Add(dr.Name);

}
}
private void btndrive_Click(object sender, EventArgs e)
{

listBox1.Items.Clear();
DriveInfo dr = new DriveInfo("d:\\");
richTextBox1.Text = "Name = " + dr.Name;
richTextBox1.Text += "\n Free space = " + dr.TotalFreeSpace;
richTextBox1.Text += "\n Size= " + dr.TotalSize;
}

0 comments:

Post a Comment