我已将文件写入指定的文件夹。将文件写入文件夹后,我将该文件附加到邮件中。将该文件附加到邮件后,我想删除该文件夹。但是没有删除该文件夹,它会抛出异常,因为“进程无法访问该文件,因为它正被另一个进程使用”
这是我的代码。
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 :(得分:0)
这是因为您通过邮件发送的文件仍未在接收方端下载。即使通过Skype发送文件甚至复制到USB记忆棒,也会发生这种情况。确保文件在接收方的末尾下载