notepad C# - 如果我再次启动应用程序,如何在关机后恢复数据 [英] notepad C#-How to restore data after shutdown, if I launch again the app

查看:214
本文介绍了notepad C# - 如果我再次启动应用程序,如何在关机后恢复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy。我正在用C#编写一个简单的记事本。如果我重新启动计算机并再次启动应用程序,它应该告诉我在关机之前写的是什么。你知道如何实现这个吗?

解决方案

按照建议,保存数据并重新加载。这应该可以帮助您确定存储位置:我应该在哪里存储数据? [ ^ ]



但存储它非常简单:

 File.WriteAllText(path,stringOfData); 
File.WriteAllLines(path,arrayOfStringsOfData);

取决于你在应用程序中的数据。

读取它的类似方法非常明显。


1)首先,强制执行自动保存。至少在临时文件中,直到用名称保存。如果你想获得最大回报,这应该尽可能频繁地完成。

2)你可以注册到系统事件(参见:http://adavesh.blogspot.hu/2011/12/handling-system-shutdown-logoff-events.html [<一个href =http://adavesh.blogspot.hu/2011/12/handling-system-shutdown-logoff-events.htmltarget =_ blanktitle =New Window> ^ ]),但如果它是关键任务,我认为这还不够。您应该按计划和/或空闲进行自动保存。

3)您应该在注册表中有一个密钥,用于存储最后编辑的文件 - 包括临时文件,所以你可以在程序启动时重新加载它们。


Hy.I'm writing a simple notepad in C# .If I restart the computer and launch again the app ,it should show me what it was written before shutdown.Do you have any idea how to implement this?

解决方案

As has been suggested, save your data and reload it. This should help you decide where to store it: Where should I store my data?[^]

But storing it is pretty easy:

File.WriteAllText(path, stringOfData);
File.WriteAllLines(path, arrayOfStringsOfData);

depending on your data within the application.
Similar methods for reading it back are pretty obvious.


1) First of all, enforce autosave. At least in a temporary file until it is saved with name. This should be done as often as possible if you want to get the most back.
2) You can register to system events (see: http://adavesh.blogspot.hu/2011/12/handling-system-shutdown-logoff-events.html[^]), but I think this is not enough if it is mission-critical. You should do autosave on schedule and/or idle.
3) You should have a key in the registry where you store the last edited file(s) - including the temporary one(s), so you can reload them when the program is started.


这篇关于notepad C# - 如果我再次启动应用程序,如何在关机后恢复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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