Skip to content

Commit

Permalink
fix possible crash (?) due to DateTime format
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed Feb 18, 2018
1 parent 354bbfc commit a20b471
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
32 changes: 28 additions & 4 deletions QuickLook.Common/Helpers/SettingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ public static void Set(string id, object value, Assembly calling = null)
private static T GetSettingFromXml<T>(XmlDocument doc, string id, T failsafe)
{
var v = doc.SelectSingleNode($@"/Settings/{id}");
var result = v == null ? failsafe : (T) Convert.ChangeType(v.InnerText, typeof(T));

return result;
try
{
var result = v == null ? failsafe : (T) Convert.ChangeType(v.InnerText, typeof(T));
return result;
}
catch (Exception)
{
return failsafe;
}
}

private static void WriteSettingToXml(XmlDocument doc, string id, object value)
Expand All @@ -81,7 +88,7 @@ private static void WriteSettingToXml(XmlDocument doc, string id, object value)
{
var node = doc.CreateNode(XmlNodeType.Element, id, doc.NamespaceURI);
node.InnerText = value.ToString();
doc.SelectSingleNode(@"/Settings").AppendChild(node);
doc.SelectSingleNode(@"/Settings")?.AppendChild(node);
}

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

var doc = new XmlDocument();
doc.Load(file);
try
{
doc.Load(file);
}
catch (XmlException)
{
CreateNewConfig(file);
doc.Load(file);
}

if (doc.SelectSingleNode(@"/Settings") == null)
{
CreateNewConfig(file);
doc.Load(file);
}

FileCache.Add(file, doc);
return doc;
}
Expand All @@ -110,6 +132,8 @@ private static void CreateNewConfig(string file)
writer.WriteStartElement("Settings");
writer.WriteEndElement();
writer.WriteEndDocument();

writer.Flush();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions QuickLook/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ private void Application_Startup(object sender, StartupEventArgs e)

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

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

private void RemoteCallShowPreview(StartupEventArgs e)
Expand Down

0 comments on commit a20b471

Please sign in to comment.