gpt4 book ai didi

c# - FileSystemWatcher 丢失文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:38:48 25 4
gpt4 key购买 nike

我是 c# 的新手,正在编写一个程序,该程序将使用从名为 folderWatch 的方法调用的 fileSystemWatcher 来监视文件夹中的 .xml 文件。 .xml 文件包含一个电子邮件地址和一个图像路径,一旦阅读,图像就会通过电子邮件发送。如果我一次只添加几个 xml,我的代码可以正常工作,但是当我尝试将大量 xml 转储到文件夹 fileSystemWatcher 时,它不会处理所有这些。请帮我指明正确的方向。

private System.IO.FileSystemWatcher m_Watcher;
public string folderMonitorPath = Properties.Settings.Default.monitorFolder;

public void folderWatch()
{
if(folderMonitorPath != "")
{
m_Watcher = new System.IO.FileSystemWatcher();
m_Watcher.Filter = "*.xml*";
m_Watcher.Path = folderMonitorPath;
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
m_Watcher.EnableRaisingEvents = true;
}
}

public void OnChanged(object sender, FileSystemEventArgs e)
{
displayText("File Added " + e.FullPath);
xmlRead(e.FullPath);
}

读取xml

    public void xmlRead(string path)
{

XDocument document = XDocument.Load(path);
var photo_information = from r in document.Descendants("photo_information")
select new
{
user_data = r.Element("user_data").Value,
photos = r.Element("photos").Element("photo").Value,
};
foreach (var r in photo_information)
{
if (r.user_data != "")
{
var attachmentFilename = folderMonitorPath + @"\" + r.photos;
displayText("new user data " + r.user_data);
displayText("attemting to send mail");
sendemail(r.user_data, attachmentFilename);
}
else
{
displayText("no user data moving to next file");
}
}

发送邮件

public void sendemail(string email, string attachmentFilename)
{
//myTimer.Stop();

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(smtpClient);

mail.From = new MailAddress(mailFrom);
mail.To.Add(email);
mail.Subject = "test";
mail.Body = "text";

SmtpServer.Port = smtpPort;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
// SmtpServer.UseDefaultCredentials = true;

if (attachmentFilename != null)
{
Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName(attachmentFilename);
disposition.Size = new FileInfo(attachmentFilename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
mail.Attachments.Add(attachment);
}
try
{
SmtpServer.Send(mail);
displayText("mail sent");
}
catch (Exception ex)
{
displayText(ex.Message);

}

}

最佳答案

首先,FileSystemWatcher 有内部限制 buffer存储未决通知。根据文档:

The system notifies the component of file changes, and it stores those changes in a buffer the component creates and passes to the APIs. Each event can use up to 16 bytes of memory, not including the file name. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory

您可以通过将 InternalBufferSize 设置为 64 * 1024(64KB,最大允许值)来增加该缓冲区。

接下来(也许更重要)是如何清除此缓冲区。您的 OnChanged 处理程序被调用,并且仅当它完成时 - 通知从该缓冲区中删除。这意味着如果您在处理程序中做了很多工作 - 缓冲区溢出的可能性要高得多。为避免这种情况 - 在 OnChanged 处理程序中尽可能少地工作,并在单独的线程中完成所有繁重的工作,例如(不是生产就绪代码,仅用于说明目的):

var queue = new BlockingCollection<string>(new ConcurrentQueue<string>());
new Thread(() => {
foreach (var item in queue.GetConsumingEnumerable()) {
// do heavy stuff with item
}
}) {
IsBackground = true
}.Start();
var w = new FileSystemWatcher();
// other stuff
w.Changed += (sender, args) =>
{
// takes no time, so overflow chance is drastically reduced
queue.Add(args.FullPath);
};

您也没有订阅 FileSystemWatcherError 事件,因此您不知道何时(以及是否)出现问题。

关于c# - FileSystemWatcher 丢失文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com