进程无法访问该文件,因为另一个进程正在使用该文件(删除文件夹)
The process cannot access the file because it is being used by another process (To delete a folder)
我已将一个文件写入指定的文件夹。把它写进文件夹后,我把那个文件附加到邮件中。将该文件附加到邮件后,我想删除该文件夹。但是文件夹没有被删除,它抛出异常,称为"该进程无法访问该文件,因为它正被另一个进程使用"
这是我的密码。
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盘,也会发生这种情况。确保文件已下载到接收器端
相关文章:
- 如何播放部分音频文件
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 如何通过子进程连接Java应用程序后在node.js中编写文件
- jar文件的后台进程,如何在进程结束时异步通知我
- 使用gulp和browserfy从脚本文件进程访问Jquery库
- Ajax 不会将登录表单的数据传递给 PHP 进程文件
- 进程无法访问该文件,因为另一个进程正在使用该文件(删除文件夹)
- 谷歌屏蔽了我的网站:所有*.js文件都被感染了.如何找到感染我的JavaScript代码的进程
- 无法使用进程.获取~/中的环境变量.配置文件或~/.Mac中的bash_profile
- Gulp-stylus不编译文件,进程内存不足
- 文件上传与进程栏使用Iframe为IE
- Node . js检查文件是否正在被其他进程使用
- 将asar包中的打包JS文件传递给生成的节点子进程
- Nodejs进程锁定从阻塞FUSE文件读取
- 为什么Nightwatch将每个.js文件都作为子进程运行?(也许我更改了一些配置)
- 节点 js 子进程删除多个文件
- 检查进程A写入的文件是否可以被进程B读取