C# File Operations: Part 2

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();
            }
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all