悬赏园豆:30
[已解决问题]
解决于 2008-07-09 07:27
<P> 现在做一个发邮件程序,用wse上传附件,不过上传后的文件我希望删掉或者重新上传就会发生该进程无法访问文件,因为该文件正由另一进程使用的问题;代码如下:</P>
<P> [WebMethod]<BR> public void UpLoadFile(string FileName, byte[] buffer, long Offset)<BR> {<BR> string FilePath = Path.Combine(_uploadPath, FileName);</P>
<P> if (Offset == 0)<BR> {<BR> File.Create(FilePath).Close();<BR> }<BR> using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))<BR> {<BR> fs.Seek(Offset, SeekOrigin.Begin);<BR> fs.Write(buffer, 0, buffer.Length);<BR> }<BR> }</P>
<P>我觉得资源都释放了啊,为什么发生这个问题?而且手动删除的时候也删不了。</P>
<P>不过如果项目是网站类型好像就没问题,是web application类型就发生上面的问题。</P>
<P> </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);
发送成功之后该文件就访问不了了?????