Wednesday 13 June 2012

copy file from one folder to other in asp.net


 StringBuilder sb = new StringBuilder(); //  Builder to save report
           string ZipFileName = String.Format(Server.MapPath("pathfto" + DateTime.Now.ToString("yyyyMMdd") + "MyZip.zip")); //  Zip Destiny File Name
           string theDirectory = Server.MapPath("pathfrom/");     //  Folder to zip

        try
        {
            sb.Append(String.Format("Directory To Zip: {0}.<br/>", theDirectory));
            sb.Append(String.Format("Zip file: {0}.<br/>", ZipFileName));
           
            string[] allFiles = Directory.GetFiles(Server.MapPath("path"), "*.*",SearchOption.AllDirectories);   // Get all files from
            // the folder to zip

           
            foreach (string s in allFiles)
            {
                // Use static Path methods to extract only the file name from the path.
              string   fileName = System.IO.Path.GetFileName(s);
              string destFile = System.IO.Path.Combine(theDirectory, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
            if (System.IO.File.Exists(ZipFileName)) //  Small piece of code
            // to delete zip file if it already exists
            {              
                System.IO.File.Delete(ZipFileName);
                sb.Append(String.Format("Deleted old Zip file: {0}.<br/>", ZipFileName));
            }

        }
        catch (Exception) { }
        }

No comments:

Post a Comment

Contact Us:

Email:

Vinodkumar434@gmail.com,
vinodtechnosoft@gmail.com

Skype Name:

vinodtechnosoft