ARTICLE

C# File Operations: Part 2

Posted by Ghanashyam Nayak Articles | Files, Directory, IO 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
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
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter