如何解决无法确定错误原因“文件正在被另一个进程使用”
这里是 C# 代码:
程序.cs
string path = @"E:\Example.txt";
if (!File.Exists(path))
{
File.Create(path);
}
// smtp code for sending emails
SmtpClient clientDetails = new SmtpClient();
...
MailMessage mailDetails = new MailMessage();
...
Attachment attachment = new Attachment(@"E:\Example.txt"); // Error "The process can not access the file because it is being used by another process" occurs here
mailDetails.Attachments.Add(attachment);
clientDetails.Send(mailDetails);
问题:仅当“Example.txt”文件不存在时才会发生错误(该进程无法访问该文件,因为它正被另一个进程使用)。意思是,仅当 File.Create(path);
创建“Example.txt”文件然后smtp 应该发送此文件时才会发生错误。因此,我假设 File.Create()
即使在文件创建完成后仍在使用该文件。请帮助我阻止它在创建后使用 txt 文件。
预先感谢您提供的各种帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。