5,317,598 members and growing! (26,735 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, C#, .NET, Windows, WinXPVS2005, Visual Studio, Dev

Posted: 31 May 2007
Updated: 31 May 2007
Views: 23,865
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.38 Rating: 3.06 out of 5
1 vote, 16.7%
1
1 vote, 16.7%
2
2 votes, 33.3%
3
1 vote, 16.7%
4
1 vote, 16.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
Screenshot of App

Introduction

To use this, you'll need to add 3 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 3 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);
            }
        }

License

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

About the Author

mariocatch


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
Questionproblem in file deletionmemberNeetu Maheshwari0:17 22 Feb '08  
QuestionFile List boxmemberMarshall S Porter13:28 6 Jul '07  
GeneralPrettymembersk8er_boy28722:40 31 May '07  
GeneralNot Too BadmemberMatrixCoder20: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:
Copyright 2007 by mariocatch
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project