Monday, 27 May 2013

How to move file from one folder to another folder using C# Code

How to move file from one folder to another folder using C# Code


Step 1: Create your website first in asp.net and create one default.aspx page.

Step 2: Open file source code .cs file or .vb file and paste below code in that file


//add System.IO package
using System.IO


// specify the path of images
string imageSourceFolderLocation= @"c:\imageFolder\";
string imageDestinationFolderLocation= @"c:\imageFolder\";


// get all files from the specify location by using Directory inbuilt library.
string[] Imagefiles = Directory.GetFiles(imageSourceFolderLocation);



//print the all file location to your page using response.write command
 for (int i = 0; i < Imagefiles .Length; i++)
        {
            string sourseFile = destination [i];
            string destination = imageDestinationFolderLocation+ Path.GetFileName(destination[i]);
            //check is file is already exist at destination location
            bool exist = File.Exists(destination);
            //check if file exist if false then
            if (exist == false)
                File.Copy(sourseFile , destination );
            Response.Write("File is copy successfully : - " + destination[i]);
        }

Step 3 : This way you can access all files from specific folder and you can do copy to another folder transaction..etc








No comments:

Post a Comment

Thank you for your interest .