2014-10-31 90 views
0

我已经将文件写入了指定的文件夹。将它写入文件夹后,我将该文件附加到邮件。将该文件附加到邮件后,我想要删除该文件夹。但文件夹未被删除,并抛出异常为“该进程无法访问该文件,因为它正在被另一个进程使用”进程无法访问该文件,因为它正在被另一个进程使用(要删除文件夹)

这是我的代码。

 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); 
    } 
+0

有什么问题? – 2014-10-31 05:28:55

+0

我为写入创建的文件夹未被删除。相反,它会引发异常:进程无法访问文件,因为它正在被另一个进程使用。 – user3687566 2014-10-31 05:35:54

回答

0

这是因为您通过邮件发送的文件仍然没有在接收端被下载。甚至在通过Skype发送文件甚至复制到USB记忆棒时也会发生这种情况。确保文件已下载到接收端

+0

但用户可能无法下载邮件。怎么处置?最新的解决方案? – user3687566 2014-10-31 05:37:31

+0

@ user3687566我相信收件人并不是邮件被使用者的用户。应首先将文件上传到邮件服务器。 – 2014-10-31 05:39:06

+0

物理上转到您发送邮件的邮件帐户,删除发送的邮件。 – SanyTiger 2014-10-31 05:40:37

相关问题