SIGN UP MEMBER LOGIN:    
ARTICLE

C# File Operations: Part 2

Posted by Ghanashyam Nayak Articles | Files, Directories in C# April 27, 2011
In this article we will see additional file operations like delete a file, copy a file & move a file from source to destination.
Reader Level:
Download Files:
 

This article describes file operations.

Here we perform the three operations Delete, Copy & Move File.

First of all is the basic idea of this article..

OpenFileDialog ofd = new OpenFileDialog();

ofd.ShowDialog();

string myFileName = ofd.FileName;


What happens when the above code is used? Using the above code it will open a OpenFileDialog Box & from that the user can select a particular file.

Using the last line it will store the full path of selected file.

See below image:
 

OpenFileDialog.JPG
 

FolderBrowserDialog fbd = new FolderBrowserDialog();

fbd.ShowDialog();

string destpath = fbd.SelectedPath;


Also using the above code it will open a "FolderBrowserDialog" box to select a path. Using the last line it will store the full path of the selected folder.

See the image below: 
 
FolderBrowserDialog.JPG

ofd.Dispose();

fbd.Dispose();

It will release all resources, which were used by "ofd" & "fbd".

ofd.SafeFileName;

It will give only filename with extension instead of full path.

System.IO.File.Delete(myFileName);

It will delete the particular given file from the given path.


System.IO.File.Copy(sourcepath, destpath + "\\" + ofd.SafeFileName);

It will copy the file from the source path to the destination path using the same name & if the name is already at destination path then it will give us a message.

 System.IO.File.Move(sourcepath, destpath + "\\" + ofd.SafeFileName);


It will move the file from the source path to the destination path using the same name & if the name is already at destination path then it will give us a message.


Main Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyBlog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
           
btnDeleteFile.Click += new EventHandler(btnDeleteFile_Click);
            btnCopyFile.Click += new EventHandler(btnCopyFile_Click);
            btnMoveFile.Click += new EventHandler(btnMoveFile_Click);
        }
        private void btnDeleteFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            string myFileName = ofd.FileName;
            try
            {
                System.IO.File.Delete(myFileName);
                MessageBox.Show("Selected File \"" + myFileName + "\" Deleted Successfully.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                ofd.Dispose();
            }
        }

        private void btnCopyFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            string sourcepath = ofd.FileName;

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            string destpath = fbd.SelectedPath;
            try
            {
                System.IO.File.Copy(sourcepath, destpath + "\\" + ofd.SafeFileName);
                MessageBox.Show("File Copied Successfully.");
            }
            catch (Exception ex)
            {
               
MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                ofd.Dispose();
                fbd.Dispose();
            }
        }

        private void btnMoveFile_Click(object sender, EventArgs e)
        {
           
OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            string sourcepath = ofd.FileName;

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            string destpath = fbd.SelectedPath;
            try
            {
                System.IO.File.Move(sourcepath, destpath + "\\" + ofd.SafeFileName);
                MessageBox.Show("File Moved Successfully.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                ofd.Dispose();
                fbd.Dispose();
            }
        }
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 

Hey Dude....First Of All I wanna say sorry for late reply... now about your question... using this code you can programatically add any event in your application... using new EventHandler It will add your given event to your application...

Posted by Ghanashyam Nayak Nov 16, 2011

thank you alot. it was very helpfull. pls. i have a question: What is function of these codes? and have they relation of (System.Data.SqlClient)?? .............................. btnDeleteFile.Click += new EventHandler(btnDeleteFile_Click); .......... btnCopyFile.Click += new EventHandler(btnCopyFile_Click); ......... btnMoveFile.Click += new EventHandler(btnMoveFile_Click);

Posted by Nasser Aug 27, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor