I have two files (.mdf
and .ldf
) in the App_Data
folder. Now I want to copy these 2 files and need to paste into backup folder while running the application. But I'm getting an error:
The process cannot access the file 'D:\App_Data\' because it is being used by another process.
These is my code I've been using
string dir = Directory.GetDirectories(@"D:\","App_data").FirstOrDefault();
string targetPath = @"D:\Back_up_PayRoll\";
if (System.IO.Directory.Exists(dir))
{
string[] files = System.IO.Directory.GetFiles(dir);
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
foreach (string s in files)
{
var fileName = System.IO.Path.GetFileName(s);
var destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
MessageBox.Show("BACK-UP Done..");
}
}