Getting System.IO.IOException when moving a file to a newly created directory

I have some code to create a new folder and move a file to that folder using FileSystemWatcher. But it gives the following error.

System.IO.IOException: The process cannot access the file because it is feeding us 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 (sender object, FileSystemEventA rgs e)

Below 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 ...

+3


source to share


2 answers


in the folder below

DirectoryInfo d = new DirectoryInfo(@"E:\SCN_DOCS\RENEWAL\")

      

any new file created using your c # application, if yes than you need to delete the newly created file object like



Bitmap bitmap = new Bitmap();
// your Image file creation code...
bitmap.Dispose();

      

as per your question that you are working with a .pdf file, so if you created a new PDF file in the above folder just before moving the file, you will need to delete that newly created pdf file object.

see this answer fooobar.com/questions/2251957 / ...

0


source


This can happen if the file is still open in the program that generated the PDF (i.e. the program that placed it in the folder).

When working with an observer in a directory, I suggest you:



  • Put all viewed files in some kind of list.
  • Use Timer

    and process files that were listed for X seconds.
  • If the file cannot be accessed, place it at the end of the list.

This way you get a more forgiving solution and you are less likely to miss files due to errors.

0


source







All Articles