Click here to Skip to main content
Licence CPOL
First Posted 31 May 2007
Views 82,685
Downloads 1,075
Bookmarked 42 times

How To Copy/Delete/Browse Files

By | 31 May 2007 | Article
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

License

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

About the Author

mariocatch

Software Developer
Electronic Arts
United States United States

Member

I graduated from Fullsail University with a degree in Game Design and Development (Computer Science).
 
After college I got a job at Electronic Arts as a QA Tester. During the QA Testing job I off-tasked and wrote some tools to help the QA Department. This work was noticed and I interviewed for a job with an automation team and got the job.
 
My day in/out work consists of tasks such as:
Working with C#/ASP.NET/SQL/WPF/C++.
 
I have a deep passion for Managed languages, notably the .NET Framework.
 
I have a desire to learn WPF inside and out as much as some of the experts that have offered their wisdom and insight here on codeproject.
 
I was a Lead Programmer for a Half-Life 2 Mod named Goldeneye: Source (www.goldeneyesource.com)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPause copy PinmemberNeCroFire3:13 26 Jan '09  
GeneralThank you!! PinmemberAdmin8876:56 14 Aug '08  
Questionproblem in file deletion PinmemberNeetu Maheshwari23:17 21 Feb '08  
hello i m deleting a file but it is giving the error that "The process cannot access the file 'D:\MyDataBase\106.jpg' because it is being used by another process."
if (PicName != null)
{
if (File.Exists(DBPicPath))
{
if (File.GetAttributes(DBPicPath) == FileAttributes.ReadOnly)
File.SetAttributes(DBPicPath, FileAttributes.Normal);
 
File.Delete(DBPicPath);
}
File.Copy(PicName, completePath, true);
}
plzzz help me....
AnswerRe: problem in file deletion Pinmemberdragan.jovanoski23:41 3 Aug '08  
QuestionFile List box PinmemberMarshall S Porter12:28 6 Jul '07  
GeneralPretty Pinmembersk8er_boy28721:40 31 May '07  
GeneralNot Too Bad PinmemberMatrixCoder19:31 31 May '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 1 Jun 2007
Article Copyright 2007 by mariocatch
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid