首页 新闻 搜索 专区 学院

该进程无法访问文件,因为该文件正由另一进程使用?

0
悬赏园豆:30 [已解决问题] 解决于 2008-07-09 07:27
<P>&nbsp; 现在做一个发邮件程序,用wse上传附件,不过上传后的文件我希望删掉或者重新上传就会发生该进程无法访问文件,因为该文件正由另一进程使用的问题;代码如下:</P> <P>&nbsp;[WebMethod]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void UpLoadFile(string FileName, byte[] buffer, long Offset)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string FilePath = Path.Combine(_uploadPath, FileName);</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (Offset == 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File.Create(FilePath).Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fs.Seek(Offset, SeekOrigin.Begin);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fs.Write(buffer, 0, buffer.Length);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P> <P>我觉得资源都释放了啊,为什么发生这个问题?而且手动删除的时候也删不了。</P> <P>不过如果项目是网站类型好像就没问题,是web application类型就发生上面的问题。</P> <P>&nbsp;</P>
问题补充: 我刚刚测试了一下,上面的代码其实没问题。问题是我发完附件后,该文件就无法访问了,应该怎么解决 //附件 System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); if (attach != null) { string directory = GetDirectory(); string filePath = string.Empty; string cid = string.Empty; System.Net.Mail.Attachment attachment = null; int i = 0; foreach (string path in attach) { filePath = Path.Combine(directory, Path.GetFileName(path)); attachment = new System.Net.Mail.Attachment(filePath); if (content.Contains(path)) { cid = String.Format("image_{0}", i); attachment.ContentId = cid; attachment.Name = Path.GetFileNameWithoutExtension(path); mailMessage.Attachments.Add(attachment); content = content.Replace(path, String.Format("cid:{0}", cid)); i++; } else { mailMessage.Attachments.Add(attachment); } } } SmtpClient.Send(mailMessage); 发送成功之后该文件就访问不了了?????
TG.Yang's IT Space的主页 TG.Yang's IT Space | 初学一级 | 园豆:45
提问于:2008-07-08 17:11
< >
分享
最佳答案
0
Choose other constructors of class System.Net.Mail.Attachment: http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.attachment.aspx Read the file content into a byte array yourself, pass the content to Attachment with a System.IO.MemoryStream wrapped around the byte array. This way System.Net.Mail won't have any chance to touch the file, so it's on your call when to close the file or delete it or do whatever you'd like to.
deerchao | 大侠五级 |园豆:8167 | 2008-07-09 00:22
其他回答(1)
0
过一段时间按时间统一删除,可能的原因是发邮件的进程还在读这个文件。
玉开 | 园豆:8822 (大侠五级) | 2008-07-08 18:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册