Click here to Skip to main content
6,595,444 members and growing! (19,131 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » General     Intermediate License: The Code Project Open License (CPOL)

How To Copy/Delete/Browse Files

By mariocatch

Browse for a Source File, and either Copy it to a Destination location, or delete that file
C# 2.0, .NET, WinXPVS2005, Dev
Version:2 (See All)
Posted:31 May 2007
Views:50,780
Bookmarked:37 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.59 Rating: 3.06 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
3 votes, 42.9%
3
1 vote, 14.3%
4
1 vote, 14.3%
5
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


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)
Occupation: Software Developer
Company: Electronic Arts
Location: United States United States

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralPause copy PinmemberNeCroFire4:13 26 Jan '09  
GeneralThank you!! PinmemberAdmin8877:56 14 Aug '08  
Questionproblem in file deletion PinmemberNeetu Maheshwari0:17 22 Feb '08  
AnswerRe: problem in file deletion Pinmemberdragan.jovanoski0:41 4 Aug '08  
QuestionFile List box PinmemberMarshall S Porter13:28 6 Jul '07  
GeneralPretty Pinmembersk8er_boy28722:40 31 May '07  
GeneralNot Too Bad PinmemberMatrixCoder20:31 31 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 May 2007
Editor: Deeksha Shenoy
Copyright 2007 by mariocatch
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project