如果在wpf中发生了一些变化,则在1分钟内保存文件# [英] save the file in 1 min interval if some changes happened in wpf c#

查看:64
本文介绍了如果在wpf中发生了一些变化,则在1分钟内保存文件#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,我必须在wpf中的richtextbox中加载。如果在txt文件中发生某些变化,则在1分钟的时间间隔后它将自动保存在不同的路径中。让第一次更改发生在10:11,然后所有更改到10:12将保存。文件创建时间将是10:12然后再次richtextbox将在10:50修改,然后文件修改时间将是10: 。加载和保存路径不同。



private void startSaveTimer()

{

计时器saveTimer = new Timer(60000);

saveTimer.Elapsed + = saveTimer_Elapsed;

saveTimer.Start();

}



private void saveTimer_Elapsed(object sender,ElapsedEventArgs e)

{

string filepath = @C:\ mydoc。 txt; //文件保存路径

if(File.Exists(filepath))

{

FileStream file = new FileStream(filepath, FileMode.Open);

//从文件中获取内容并与richtextbox数据进行比较

//如果不相同则再次保存

}

else

{

//保存文件

}

} < br $>


private void richText_TextChanged(object sender,TextChangedEventArgs e)

{

startSaveTimer();

}

但是每个单元中调用的代码都改变了,所以

FileStream file = new FileStream(filepath,FileMode.Open);显示不同用户使用的错误。试着保持锁定仍然有同样的错误。

提前谢谢

I have a xml file which i have to load in richtextbox in wpf. if some changes occure in txt file then it will save in different path automatically after a time interval of 1 min. Let the 1st changes happens in 10:11 then all the changes made up to 10:12 will save.File creation time will be 10:12 then again the richtextbox will be modified at 10:50 then the file modified time will be 10:51.The loading and the saving path is different.

private void startSaveTimer()
{
Timer saveTimer = new Timer(60000);
saveTimer.Elapsed += saveTimer_Elapsed;
saveTimer.Start();
}

private void saveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
string filepath = @"C:\mydoc.txt";//file save path
if (File.Exists(filepath))
{
FileStream file = new FileStream(filepath, FileMode.Open);
//geting the content from file and compare with richtextbox data
//if not same then saving it again
}
else
{
//save file
}
}

private void richText_TextChanged(object sender, TextChangedEventArgs e)
{
startSaveTimer();
}
but this code called in each single changed so
FileStream file = new FileStream(filepath, FileMode.Open); is showing error as used by different user. try to keep lock still got the same error.
Thanks in advance

推荐答案





您可以使用类似DispatcherTimer的东西,并将其间隔设置为60000毫秒,或者,如果不可用,则设置为1000毫秒,然后计算60次出现的方法。



那么你可以这样做:



1.使用RichTextBox控件的事件TextChanged并设置一个bool标志(对于istance,已保存)如果尚未保存,则表示未保存,然后启动计时器。

2.在计时器处理程序中,如果设置为60000毫安,则保存richtextbox并禁用计时器的。这样,如果在接下来的几分钟内没有修改,文档就不会被保存。



这是代码:

Hi,

You can use something like a DispatcherTimer and set its interval to 60000 milliseconds, or, if not available, 1000 milliseconds and then count 60 occurrences of the method.

Then you can do this way:

1. Use the event TextChanged of the RichTextBox control and set a bool flag (called, for istance, Saved) to false to indicate that it is not saved and then start the timer if not already started.
2. In the timer handler, if set to 60000 millis, save the richtextbox and disable the timer. This way the document does not get saved if no modifies are made in the next minutes.

Here is the code:
FileStream file = new FileStream(filepath, FileMode.Open);

Timer saveTimer = new Timer(60000);
private void startSaveTimer()
{

saveTimer.Elapsed += saveTimer_Elapsed;
saveTimer.Start();
}
private bool saved=true;
private void saveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
string filepath = @"C:\mydoc.txt";//file save path
if (!saved)
{
//save file
saved=true;
timer.enabled=false;
}
}

private void richText_TextChanged(object sender, TextChangedEventArgs e)
{
saved=false
if(!timer.enabled)
timer.enabled=true;
}





我希望这有帮助...



PS:我建议你获取当前的RichTextBox内容并将其传递给另一个线程,以便在你保存时不阻止用户界面...这可以为你节省一些麻烦:)



LG



I hope this helps...

PS: I advise you to get the current RichTextBox content and pass it in another thread in order not to block the UI while you are saving... This can save you some troubles :)

LG


这篇关于如果在wpf中发生了一些变化,则在1分钟内保存文件#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆