I got this code straight from How to: Copy, Delete, and Move Files and Folders (C# Programming Guide):
string sourceDir = currDir + "\\" + "Trends";
string targetDir = currDir + "\\" + reportDir + "\\" + "Trends";
if (Directory.Exists(sourceDir))
{
string[] files = Directory.GetFiles(sourceDir);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string targetFile = Path.Combine(targetDir, fileName);
File.Copy(file, targetFile, true);
}
}
However, when I run this code I throw an error on every file in sourceDir that it can’t be accessed because it is already in use. When I exit the code it is clear that no other processes are using these files so it must be this block of code that is causing the problem. Is there a way to use “using” for this?
Any advice is appreciated.
Regards.
Thanks for all of the suggestions — they led me to the answer. I am overriding the Render method of my web page to save one of the tables as a .html file. In order to display this table I need the files in sourceDir. However, the table doesn’t render right away so at the first call to Render those files are legitimately in use by the web app. The code I posted works just fine.