Working with directories in Windows Application


We will do coding coding for creating directories, get list of file in particular directory and also get list of directory in selected drive. During the form load list of drive is shown in the Combobox. And then we perform different operations like creating directory in a selected drive.We can get the list of directories in a selected drive. And also get list of files in the selected directory of the selected drive.

In this we have a combobox which shows list of drives during the form load.
After that we select one of the radiobutton and perform operation accordingly.
If we select to create a directory then grbx_dir visibility is set to true and we will create directory in the selected drive. If we select to get list of directory then grbx_alldir visibility is true and we get all the directory in selected drive. Similarly we get list of files if selected directory.


//To get all the drives in combobox during form load.
private void Form1_Load(object sender, EventArgs e)
{
string[] drives = Directory.GetLogicalDrives();
for(int i =0 ; i combo_drive.Items.Add(drives[i]);

grpbx_alldir.Visible = false;
grpbx_allfiles.Visible = false;
grpbx_dir.Visible = false;
grpbx_selctdircty.Visible = false;
grpbx_select.Visible = true;

}

private void radbt_dir_CheckedChanged(object sender, EventArgs e)
{
if (radbt_dir.Checked == true)
{
grpbx_dir.Visible = true;
}
else
{
grpbx_dir.Visible = false;
}
}




private void radbut_alldir_CheckedChanged(object sender, EventArgs e)
{
if (radbut_alldir.Checked == true)
{
grpbx_alldir.Visible = true;
grpbx_allfiles.Visible = false;
}
else
{
grpbx_alldir.Visible = false;
}


}
// To get all the directory of the drive selected in combobox
private void radbut_allfile_CheckedChanged(object sender, EventArgs e)
{

if (radbut_allfile.Checked == true)
{
grpbx_allfiles.Visible = true;

string d = combo_drive.SelectedItem.ToString();
string[] dirnm = Directory.GetDirectories(d);
for (int i = 0; i < dirnm.Length; i++)
{
combobx_getallfiles.Items.Add(dirnm[i].ToString());
}
}
else
{
grpbx_allfiles.Visible = false;
}
}
//To create a directory in selected drive and if it already exists it show a //message that directory already exists.
private void but_dir_Click(object sender, EventArgs e)
{
if (Directory.Exists(combo_drive.SelectedItem + txt_dir1.Text))
{
MessageBox.Show("Directory exists");
}
else
{
Directory.CreateDirectory(combo_drive.SelectedItem + txt_dir1.Text);
MessageBox.Show("created");
}
}



private void but_getalldir_Click(object sender, EventArgs e)
{
string d = combo_drive.SelectedItem.ToString();
string[] dirnm = Directory.GetDirectories(d);
for (int i = 0; i < dirnm.Length; i++)
{
lstbox_dir.Items.Add(dirnm[i].ToString());
}
}
//To get list of files in selected directory
private void combobx_getallfiles_SelectedIndexChanged(object sender, EventArgs e)
{
listBox_files.Items.Clear();
string d = combobx_getallfiles.SelectedItem.ToString();
string[] filenm = Directory.GetFiles(d);
for (int i = 0; i < filenm.Length; i++)
{
listBox_files.Items.Add(filenm[i].ToString());
}
}



Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: