mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-11 02:34:08 +02:00
Work on UserConfig (save on exit, use backup file, fix loading)
This commit is contained in:
parent
3db2d11af7
commit
099d5afa56
@ -20,7 +20,7 @@ sealed class UserConfig{
|
||||
// END OF CONFIGURATION
|
||||
|
||||
[NonSerialized]
|
||||
private readonly string file;
|
||||
private string file;
|
||||
|
||||
private UserConfig(string file){
|
||||
this.file = file;
|
||||
@ -33,6 +33,12 @@ public bool Save(){
|
||||
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
if (File.Exists(file)){
|
||||
string backupFile = GetBackupFile(file);
|
||||
File.Delete(backupFile);
|
||||
File.Move(file,backupFile);
|
||||
}
|
||||
|
||||
using(Stream stream = new FileStream(file,FileMode.Create,FileAccess.Write,FileShare.None)){
|
||||
Formatter.Serialize(stream,this);
|
||||
}
|
||||
@ -47,16 +53,26 @@ public bool Save(){
|
||||
public static UserConfig Load(string file){
|
||||
UserConfig config = null;
|
||||
|
||||
try{
|
||||
using(Stream stream = new FileStream(file,FileMode.Open,FileAccess.Read,FileShare.Read)){
|
||||
config = Formatter.Deserialize(stream) as UserConfig;
|
||||
for(int attempt = 0; attempt < 2; attempt++){
|
||||
try{
|
||||
using(Stream stream = new FileStream(attempt == 0 ? file : GetBackupFile(file),FileMode.Open,FileAccess.Read,FileShare.Read)){
|
||||
if ((config = Formatter.Deserialize(stream) as UserConfig) != null){
|
||||
config.file = file;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}catch(FileNotFoundException){
|
||||
}catch(Exception){
|
||||
// TODO
|
||||
}
|
||||
}catch(FileNotFoundException){
|
||||
}catch(Exception){
|
||||
// TODO
|
||||
}
|
||||
|
||||
return config ?? new UserConfig(file);
|
||||
}
|
||||
|
||||
private static string GetBackupFile(string file){
|
||||
return file+".bak";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ private static void Main(){
|
||||
Application.Run(new FormBrowser());
|
||||
|
||||
Application.ApplicationExit += (sender, args) => {
|
||||
UserConfig.Save();
|
||||
Cef.Shutdown();
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user