Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How To Copy/Delete/Browse Files

3.16/5 (9 votes)
31 May 2007CPOL 1   1.8K  
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

C#
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

C#
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

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)