private void OnWxsChanged() { wxsWatcher.EnableRaisingEvents = false; DialogResult result = DialogResult.None; if (undoManager.HasChanges()) { Form mainForm = FindForm(); result = MessageBox.Show(mainForm, String.Format("An external program changed \"{0}\", do you want to load the changes from disk and ignore the changes in memory?", wxsFile.Name), "Reload?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } else { Form mainForm = FindForm(); result = MessageBox.Show(mainForm, String.Format("An external program changed \"{0}\", do you want to load the changes from disk?", wxsFile.Name), "Reload?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } if (result == DialogResult.Yes) { try { LoadWxsFile(); } catch (UnauthorizedAccessException) { MessageBox.Show(String.Format("Access is denied. ({0}))", wxsFile.Name), "Acces denied", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } catch (XmlException ex) { MessageBox.Show(String.Format("Failed to open file. ({0}) The xml is not valid:\r\n\r\n{1}", wxsFile.Name, ex.Message), "Open failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } catch (WixEditException ex) { MessageBox.Show(String.Format("Cannot open file:\r\n\r\n{0}", ex.Message), "Open failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } catch { MessageBox.Show(String.Format("Failed to open file. ({0}))", wxsFile.Name), "Open failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } UndoManager.Clear(); UndoManager.DocumentIsSaved(); if (wxsChanged != null) { wxsChanged(this, new EventArgs()); } } wxsWatcher.EnableRaisingEvents = true; }
public void Save() { if (!IsNew && ReadOnly()) { MessageBox.Show(String.Format("\"{0}\" is read-only, cannot save this file.", wxsFile.Name), "Read Only!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (wxsWatcher != null) { wxsWatcher.EnableRaisingEvents = false; } UndoManager.DeregisterHandlers(); XmlComment commentElement = null; if (projectSettings.IsEmpty() == false) { StringBuilder commentBuilder = new StringBuilder(); commentBuilder.Append("\r\n"); commentBuilder.Append(" # This comment is generated by WixEdit, the specific commandline\r\n"); commentBuilder.Append(" # arguments for the WiX Toolset are stored here.\r\n\r\n"); commentBuilder.AppendFormat(" candleArgs: {0}\r\n", projectSettings.CandleArgs); commentBuilder.AppendFormat(" lightArgs: {0}\r\n", projectSettings.LightArgs); commentElement = wxsDocument.CreateComment(commentBuilder.ToString()); XmlNode firstElement = wxsDocument.FirstChild; if (firstElement != null) { if (firstElement.NodeType == XmlNodeType.XmlDeclaration) { firstElement = wxsDocument.FirstChild.NextSibling; } wxsDocument.InsertBefore(commentElement, firstElement); } else { wxsDocument.AppendChild(commentElement); } } // Handle extension namespaces. Remove all those which are not used in the file. foreach (string ext in xsdExtensionNames) { string theNodeNamespace = LookupExtensionNameReverse(ext); XmlNodeList list = wxsDocument.SelectNodes(String.Format("//{0}:*", theNodeNamespace), wxsNsmgr); if (list.Count == 0) { // Error reports show that a NullReferenceException occurs on the next line now, how can this be? // The wxsDocument.DocumentElement is null. wxsDocument.DocumentElement.RemoveAttribute(String.Format("xmlns:{0}", theNodeNamespace)); } } if (IncludeManager.HasIncludes) { IncludeManager.RemoveIncludes(); ArrayList changedIncludes = UndoManager.ChangedIncludes; if (changedIncludes.Count > 0) { string filesString = String.Join("\r\n\x2022 ", changedIncludes.ToArray(typeof(string)) as string[]); if (DialogResult.Yes == MessageBox.Show(String.Format("Do you want to save the following changed include files?\r\n\r\n\x2022 {0}", filesString), "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { IncludeManager.SaveIncludes(UndoManager.ChangedIncludes); } } } FileMode mode = FileMode.OpenOrCreate; if (File.Exists(wxsFile.FullName)) { mode = mode | FileMode.Truncate; } using (FileStream fs = new FileStream(wxsFile.FullName, mode)) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = new string(' ', WixEditSettings.Instance.XmlIndentation); settings.NewLineHandling = NewLineHandling.None; settings.Encoding = new System.Text.UTF8Encoding(); XmlWriter writer = XmlWriter.Create(fs, settings); wxsDocument.Save(writer); writer.Close(); fs.Close(); } if (IncludeManager.HasIncludes) { // Remove nodes from main xml document IncludeManager.RestoreIncludes(); } projectSettings.ChangesHasBeenSaved(); if (commentElement != null) { wxsDocument.RemoveChild(commentElement); } if (wxsWatcher != null) { wxsWatcher.EnableRaisingEvents = true; } undoManager.DocumentIsSaved(); UndoManager.RegisterHandlers(); }