进程无法访问该文件,因为另一个进程正在使用该文件(删除文件夹)

The process cannot access the file because it is being used by another process (To delete a folder)

本文关键字:进程 文件 文件夹 删除 另一个 访问 因为      更新时间:2023-09-26

我已将一个文件写入指定的文件夹。把它写进文件夹后,我把那个文件附加到邮件中。将该文件附加到邮件后,我想删除该文件夹。但是文件夹没有被删除,它抛出异常,称为"该进程无法访问该文件,因为它正被另一个进程使用"

这是我的密码。

     public HttpResponseMessage SendChannelPartenersMessage(string Name,string FirmName,string Address, string Email,string Mobile)                                        
     {
         var httpRequest = HttpContext.Current.Request;
         ContactUs contactUs = new ContactUs();
         contactUs.Address = Address;
         contactUs.Name = Name;
         contactUs.FirmName = FirmName;
         contactUs.Email = Email;
         contactUs.Mobile = Mobile;
         try
         {
             if (httpRequest.Files.Count > 0)
             {
                 contactUs.AttachFileName = WriteAttachedFile(httpRequest, contactUs.Email);
                 if (ContactUsService.SendChannelPartenersMessage(contactUs))
                 {
                     var fileToBeDeleted = contactUs.AttachFileName;
                     var deleteFile = DeleteAttachedFile(contactUs.AttachFileName);
                 }
                 return Request.CreateResponse(HttpStatusCode.OK, contactUs);
             }
             else
             {
                 return Request.CreateResponse(HttpStatusCode.BadRequest);
             }
         }
         catch (Exception e)
         {
             throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
             {
                 Content = new StringContent("An error occurred, please try again or contact the administrator."),
                 ReasonPhrase = "Critical Exception"
             });
         }
   }
    private string WriteAttachedFile(HttpRequest httpRequest, string FileName)
    {         
        var postedFile = httpRequest.Files[0];
        var directoryPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"].ToString() + FileName + "''''";
        var filePath = directoryPath + postedFile.FileName;
        Directory.CreateDirectory(directoryPath);
        postedFile.SaveAs(filePath);
        var Path = filePath.Replace("''", "/");
        return (Path);
    }   
     private bool DeleteAttachedFile(string FileName)     
     {
         if (System.IO.File.Exists(FileName))
         {
             System.IO.File.Delete(FileName);
         }
         string[] words = FileName.Split('/');
         string directoryPath = words[words.Length - 2];
         if (Directory.Exists(directoryPath))
         {
             Directory.Delete(directoryPath);
         }   
         return (true);
    }

这是因为您通过邮件发送的文件在接收方仍然没有下载。即使通过Skype发送文件,甚至复制到U盘,也会发生这种情况。确保文件已下载到接收器端