I have a code to create new folder and move file to that folder using FileSystemWatcher.But it gives following error.
System.IO.IOException: The process cannot access the file because it is being us ed by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError() at System.IO.File.Move(String sourceFileName, String destFileName) at FolderWatcher.Program.ProcessRenewalFolder(Object sender, FileSystemEventA rgs e)
Following is the code
'private static void ProcessRenewalFolder(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Renewal Received.... ");
DirectoryInfo d = new DirectoryInfo(@"E:\SCN_DOCS\RENEWAL\");
DirectoryInfo dest = new DirectoryInfo(@"E:\QUEUED_SCN_DOCS\RENEWAL\");
if (!d.Exists)
{
return;
}
FileInfo[] Files = d.GetFiles("*.pdf");
string jobNo = "";
string branchCode = "";
foreach (FileInfo file in Files)
{
jobNo = file.Name;
DirectoryInfo newDir = null;
if (!Directory.Exists(dest.FullName + jobNo.ToUpper()))
{
System.IO.Directory.CreateDirectory(dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
}
Console.WriteLine(jobNo + " - " + branchCode);
try
{
File.Move(file.FullName, dest.FullName + jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper() + "\\" + file.Name.ToUpper());
UpdateRenewal(jobNo.Substring(0, file.Name.LastIndexOf(".")).ToUpper());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}'
Please let me know the reason for this...