private void SaveFile(CrawlingContext file, bool saveAs) { FileInfo target = null; if (file.FullPath == null || saveAs) { // Configure save file dialog box SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = file.FullPath == null ? "CrawlingResult.cwl" : file.FullPath.FullName; // Default file name dlg.DefaultExt = ".cwl"; // Default file extension dlg.Filter = "Crawling result (.cwl)|*.cwl"; // Filter files by extension // Process save file dialog box results if (dlg.ShowDialog() == true) { target = new FileInfo(dlg.FileName); } else { return; } } else { target = file.FullPath; } if (target != null) { file.Status = CrawlingStatus.Saving; ThreadPool.QueueUserWorkItem(_ => { try { XElement element = ResourcesSerializer.SerializeResourceCollection(file.Resources); element.Save(target.FullName); file.FullPath = target; file.HasChanged = false; } catch (Exception e) { Dispatcher.Invoke((Action)(() => MessageBox.Show("Failed to save file " + target.Name + ": " + e.Message))); } finally { file.Status = CrawlingStatus.Ready; RefreshActions(); } }); } }
private void LoadFile(FileInfo file) { CrawlingContext context = new CrawlingContext(); context.FullPath = new FileInfo(file.FullName); context.HasChanged = false; context.Status = CrawlingStatus.Loading; AddFile(context); ThreadPool.QueueUserWorkItem(_ => { XDocument doc = XDocument.Load(file.FullName); List <Resource> resources = ResourcesSerializer.DeserializeResourceCollection(doc.Root); foreach (Resource resource in resources) { context.Resources.Add(resource); } context.Status = CrawlingStatus.Ready; RefreshActions(); }); }