Skip to content

Commit a20b471

Browse files
committed
fix possible crash (?) due to DateTime format
1 parent 354bbfc commit a20b471

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

QuickLook.Common/Helpers/SettingHelper.cs

+28-4
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ public static void Set(string id, object value, Assembly calling = null)
6464
private static T GetSettingFromXml<T>(XmlDocument doc, string id, T failsafe)
6565
{
6666
var v = doc.SelectSingleNode($@"/Settings/{id}");
67-
var result = v == null ? failsafe : (T) Convert.ChangeType(v.InnerText, typeof(T));
6867

69-
return result;
68+
try
69+
{
70+
var result = v == null ? failsafe : (T) Convert.ChangeType(v.InnerText, typeof(T));
71+
return result;
72+
}
73+
catch (Exception)
74+
{
75+
return failsafe;
76+
}
7077
}
7178

7279
private static void WriteSettingToXml(XmlDocument doc, string id, object value)
@@ -81,7 +88,7 @@ private static void WriteSettingToXml(XmlDocument doc, string id, object value)
8188
{
8289
var node = doc.CreateNode(XmlNodeType.Element, id, doc.NamespaceURI);
8390
node.InnerText = value.ToString();
84-
doc.SelectSingleNode(@"/Settings").AppendChild(node);
91+
doc.SelectSingleNode(@"/Settings")?.AppendChild(node);
8592
}
8693

8794
doc.Save(new Uri(doc.BaseURI).LocalPath);
@@ -97,7 +104,22 @@ private static XmlDocument GetConfigFile(string file)
97104
CreateNewConfig(file);
98105

99106
var doc = new XmlDocument();
100-
doc.Load(file);
107+
try
108+
{
109+
doc.Load(file);
110+
}
111+
catch (XmlException)
112+
{
113+
CreateNewConfig(file);
114+
doc.Load(file);
115+
}
116+
117+
if (doc.SelectSingleNode(@"/Settings") == null)
118+
{
119+
CreateNewConfig(file);
120+
doc.Load(file);
121+
}
122+
101123
FileCache.Add(file, doc);
102124
return doc;
103125
}
@@ -110,6 +132,8 @@ private static void CreateNewConfig(string file)
110132
writer.WriteStartElement("Settings");
111133
writer.WriteEndElement();
112134
writer.WriteEndDocument();
135+
136+
writer.Flush();
113137
}
114138
}
115139
}

QuickLook/App.xaml.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ private void Application_Startup(object sender, StartupEventArgs e)
8888

8989
private void CheckUpdate()
9090
{
91-
if (DateTime.Now - SettingHelper.Get<DateTime>("LastUpdate") < TimeSpan.FromDays(7))
91+
if (DateTime.Now.Ticks - SettingHelper.Get<long>("LastUpdateTicks") < TimeSpan.FromDays(7).Ticks)
9292
return;
9393

9494
Task.Delay(120 * 1000).ContinueWith(_ => Updater.CheckForUpdates(true));
95-
SettingHelper.Set("LastUpdate", DateTime.Now);
95+
SettingHelper.Set("LastUpdateTicks", DateTime.Now.Ticks);
9696
}
9797

9898
private void RemoteCallShowPreview(StartupEventArgs e)

0 commit comments

Comments
 (0)