【发布时间】:2019-01-26 00:22:35
【问题描述】:
/// <summary>
/// Save settings to config file. Create backup of current settings
/// </summary>
public void SaveSettings()
{
Directory.CreateDirectory(CONFIG_DIRECTORY);
// move current settings file to backup
if (File.Exists(SettingsFile))
{
if (File.Exists(SettingsBackupFile)) // delete prev backup
File.Delete(SettingsBackupFile);
File.Move(SettingsFile, SettingsBackupFile);
}
// Create new settings file
using (var f = (TextWriter)File.CreateText(SettingsFile))
{
f.WriteLine(JsonConvert.SerializeObject(_settings, Formatting.Indented));
f.Close();
}
}
我将当前设置文件重命名为 .bak 文件,然后替换当前设置文件。我这样做是因为如果用户在下载过程中关闭电源,我的文件可能/将会损坏。我认为如果设置文件已损坏,我可以从备份中读取此设计模式,但备份文件也已损坏。
【问题讨论】:
-
如果
SettingsFile被破坏,你认为File.Move(SettingsFile, SettingsBackupFile);会做什么? -
my files can/will become corrupted if the user does a power shutdown during a download但是这样做的用户应该得到发生在他们身上的一切。 -
@NoneoftheAbove 对于生活在电力供应不可靠地区的最终用户,您的看法如此富有同情心。 :)
-
user does a power shutdown不是停电。 -
@mjwills 你可能是对的。我可能错误地认为如果文件因断电而损坏,那么我的代码将不会执行,因此无法备份最近损坏的文件
标签: c# .net backup corruption file-management