65.9K
Home

How To Copy/Delete/Browse Files

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.16/5 (9 votes)

Jun 1, 2007

CPOL
viewsIcon

115910

downloadIcon

1811

Browse for a Source File, and either Copy it to a Destination location, or delete that file

Screenshot of App

Introduction

To use this, you'll need to add three controls to the Toolbox that Microsoft has disabled by default. They are amazing controls that simplify the workload of this project by a lot.

They are DriveListBox, DirListBox, and FileListBox. Simply go to Tools > Choose Toolbox Items > and check DriveListBox, DirListBox, and FileListBox. Once done, add them and go to your design view. Add the three new controls to your project (arrange to look good).

Using the Code

Using ListBoxes

private void driveListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        dirListBox1.Path = driveListBox1.Drive;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error : " + ex.Message, 
                        "Error", 
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
}
 private void dirListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        fileListBox1.Path = dirListBox1.Path;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error : " + ex.Message,
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
    }
}
 private void fileListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string fullPath = fileListBox1.Path + "\\" + fileListBox1.FileName;
    try
    {
        sr = new StreamReader(fullPath);
        richTextBox1.Text = sr.ReadToEnd();
        textBox1.Text = fullPath;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error : " + ex.Message,
        "Error",
        MessageBoxButtons.OK,
        MessageBoxIcon.Error);
    }
}

Copy File

private void button2_Click(object sender, EventArgs e)
{
    sr.Close();
    try
    {
        File.Copy(fileListBox1.Path + "\\" + fileListBox1.FileName,
                  dirListBox2.Path + "\\" + fileListBox1.FileName, true);
         richTextBox1.Clear();
        richTextBox1.Text += "Copy Succeeded\n";
     }
    catch (Exception ex)
    {
        MessageBox.Show("Error : " + ex.Message,
        "Error",
        MessageBoxButtons.OK,
        MessageBoxIcon.Error);
    }
}

Delete File

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        DialogResult dr;
        dr = MessageBox.Show("Are you sure you wish to delete " + 
			fileListBox1.FileName + "?",
                        "Confirmation",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question);
        if (dr == DialogResult.No)
            return;
        sr.Close();
        File.Delete(fileListBox1.Path + "\\" + fileListBox1.FileName);
        fileListBox1.Refresh();
        dirListBox1.Refresh();
        dirListBox2.Refresh();
        driveListBox1.Refresh();
        driveListBox2.Refresh();
        richTextBox1.Clear();
        richTextBox1.Text += "Delete Succeeded";
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error : " + ex.Message,
        "Error",
        MessageBoxButtons.OK,
        MessageBoxIcon.Error);
    }
}

History

  • 1st June, 2007: Initial post