IOException: The process cannot access the file 'file path' because it is being used by another process

前端 未结 10 2049
陌清茗
陌清茗 2020-11-22 00:13

I have some code and when it executes, it throws a IOException, saying that

The process cannot access the file \'filename\' because it

相关标签:
10条回答
  • 2020-11-22 00:22

    As other answers in this thread have pointed out, to resolve this error you need to carefully inspect the code, to understand where the file is getting locked.

    In my case, I was sending out the file as an email attachment before performing the move operation.

    So the file got locked for couple of seconds until SMTP client finished sending the email.

    The solution I adopted was to move the file first, and then send the email. This solved the problem for me.

    Another possible solution, as pointed out earlier by Hudson, would've been to dispose the object after use.

    public static SendEmail()
    {
               MailMessage mMailMessage = new MailMessage();
               //setup other email stuff
    
                if (File.Exists(attachmentPath))
                {
                    Attachment attachment = new Attachment(attachmentPath);
                    mMailMessage.Attachments.Add(attachment);
                    attachment.Dispose(); //disposing the Attachment object
                }
    } 
    
    0 讨论(0)
  • 2020-11-22 00:24

    I got this error because I was doing File.Move to a file path without a file name, need to specify the full path in the destination.

    0 讨论(0)
  • 2020-11-22 00:28

    My below code solve this issue, but i suggest First of all you need to understand what causing this issue and try the solution which you can find by changing code

    I can give another way to solve this issue but better solution is to check your coding structure and try to analyse what makes this happen,if you do not find any solution then you can go with this code below

    try{
    Start:
    ///Put your file access code here
    
    
    }catch (Exception ex)
     {
    //by anyway you need to handle this error with below code
       if (ex.Message.StartsWith("The process cannot access the file"))
        {
             //Wait for 5 seconds to free that file and then start execution again
             Thread.Sleep(5000);
             goto Start;
        }
     }
    
    0 讨论(0)
  • 2020-11-22 00:35

    Had an issue while uploading an image and couldn't delete it and found a solution. gl hf

    //C# .NET
    var image = Image.FromFile(filePath);
    
    image.Dispose(); // this removes all resources
    
    //later...
    
    File.Delete(filePath); //now works
    
    0 讨论(0)
  • 2020-11-22 00:35

    I had the following scenario that was causing the same error:

    • Upload files to the server
    • Then get rid of the old files after they have been uploaded

    Most files were small in size, however, a few were large, and so attempting to delete those resulted in the cannot access file error.

    It was not easy to find, however, the solution was as simple as Waiting "for the task to complete execution":

    using (var wc = new WebClient())
    {
       var tskResult = wc.UploadFileTaskAsync(_address, _fileName);
       tskResult.Wait(); 
    }
    
    0 讨论(0)
  • 2020-11-22 00:37

    I'm using FileStream and having same issue.. When ever Two request try to read same file it throw this exception.

    solution use FileShare

    using FileStream fs = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
    

    I'm Just Reading a file concurrently FileShare.Read solve my issue.

    0 讨论(0)
提交回复
热议问题