3
votes

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

2
have you opened the file your self somewhere else and forgot to close it? this message is generic and often misleading,because at times it's your application code that is blocking.Amit Kumar Ghosh

2 Answers

0
votes

in below folder path

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

any new file created using your c# application if yes than you need to dispose the object of newly created file for example

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

as per your question you are working with .pdf file so if you created any new pdf file in above folder just before moving file, than you need to dispose that object of newly created pdf file.

see this answer https://stackoverflow.com/a/31830176/4988990

0
votes

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

When you work with a directory watcher I suggest that you:

  1. Put all watched files in some sort of a list.
  2. Use a Timer and process files that have been in the list for X seconds.
  3. If the file can't be accessed, put it in the end of the list.

In that way you get a more forgiving solution and it's not likely that you'll miss files due to errors.