예제 #1
0
        /// <summary>
        /// Save the specified CCO options to the specified file.
        /// </summary>
        /// <param name="options">The CCO to save.</param>
        /// <param name="filePath">The path of the file to save the XML options data to.</param>
        /// <param name="errorTxt">Holds the error message of the Exception if there was an error during save.</param>
        /// <returns>True if there were no errors, otherwise false.</returns>
        public bool Save(CCO options, string filePath, out string errorTxt)
        {
            errorTxt = "";

            try {
                // Perform sanity check on CCO object
                CCOFunctions optFunc = new CCOFunctions();
                string sanityErrors;
                if (!optFunc.SanityCheck(options, out sanityErrors)) {
                    throw new Exception("Error(s) found in backup profile:\r\n" + sanityErrors);
                }

                // First, build up our XmlDocument and store configuration values
                XmlDocument doc = new XmlDocument();
                XmlElement rootElement;
                doc.AppendChild(
                    rootElement = doc.CreateElement("carbonCopyOptions")
                );

                XmlElement typeElement = doc.CreateElement("backupType");
                typeElement.SetAttribute("value", ((int)options.Type).ToString());
                rootElement.AppendChild(typeElement);

                XmlElement dryRunElement = doc.CreateElement("isDryRun");
                dryRunElement.SetAttribute("value", options.IsDryRun.ToString());
                rootElement.AppendChild(dryRunElement);

                XmlElement displayElement = doc.CreateElement("outputDetail");
                displayElement.SetAttribute("value", ((int)options.OutputDetail).ToString());
                rootElement.AppendChild(displayElement);

                XmlElement srcElement = doc.CreateElement("srcDirs");
                foreach (DirectoryInfo di in options.SourceDirs) {
                    XmlText dirTxt = doc.CreateTextNode(di.FullName);
                    XmlElement srcDir = doc.CreateElement("dir");
                    srcDir.AppendChild(dirTxt);
                    srcElement.AppendChild(srcDir);
                }
                rootElement.AppendChild(srcElement);

                XmlElement destElement = doc.CreateElement("destDir");
                destElement.SetAttribute("value", options.DestDir.FullName);
                rootElement.AppendChild(destElement);

                // Grab an XPathNavigator to navigate thru our entire XmlDocument tree
                XPathNavigator nav = doc.CreateNavigator();

                // Create the XmlWriter that will write the XML document to disk
                XmlWriterSettings sett = new XmlWriterSettings();
                sett.Indent = true;
                sett.IndentChars = "\t";
                XmlWriter writer = XmlWriter.Create(filePath, sett);
                // Finally write it, and close the writer
                nav.WriteSubtree(writer);
                writer.Close();
            }
            catch (Exception ex) {
                errorTxt = ex.Message;
                return false;
            }

            return true;
        }
예제 #2
0
        /// <summary>
        /// Load the CCO options data from the specified XML file and supply it as a filled CCO object.
        /// </summary>
        /// <param name="options">The CCO options data as a CCO object.</param>
        /// <param name="filePath">The path of the file to load the XML options data from.</param>
        /// <param name="errorTxt">Holds the error message of the Exception if there was an error during load.</param>
        /// <returns>True if there were no errors, otherwise false.</returns>
        public bool Load(out CCO options, string filePath, out string errorTxt)
        {
            errorTxt = "";
            options = new CCO();

            try {
                CCOFunctions optFunc = new CCOFunctions();

                // First, create our XmlDocument for storing configuration values
                XmlDocument doc = new XmlDocument();

                // Create the XmlReader that will read the XML document from disk
                XmlReader reader = XmlReader.Create(filePath);

                // Read and load it, and close the reader
                doc.Load(reader);
                reader.Close();

                // Create an XPathNavigator so we can easily query values from the XmlDocument
                XPathNavigator nav = doc.CreateNavigator();

                // Now populate the CCO object from our XmlDocument's values
                // Backup type
                XPathNodeIterator typeIter = nav.Select("/carbonCopyOptions/backupType");
                if (!typeIter.MoveNext()) {
                    throw new Exception("Couldn't find backupType configuration entry!");
                }
                options.Type = (CCOTypeOfBackup)Convert.ToInt32(typeIter.Current.GetAttribute("value", ""));

                // Is dry run?
                XPathNodeIterator dryRunIter = nav.Select("/carbonCopyOptions/isDryRun");
                if (!dryRunIter.MoveNext()) {
                    throw new Exception("Couldn't find isDryRun configuration entry!");
                }
                options.IsDryRun = Convert.ToBoolean(dryRunIter.Current.GetAttribute("value", ""));

                // To display
                XPathNodeIterator displayIter = nav.Select("/carbonCopyOptions/outputDetail");
                if (!displayIter.MoveNext()) {
                    throw new Exception("Couldn't find outputDetail configuration entry!");
                }
                options.OutputDetail = (VerbosityLevel)Convert.ToInt32(displayIter.Current.GetAttribute("value", ""));

                // Source dirs
                options.SourceDirs = new List<DirectoryInfo>();
                XPathNodeIterator srcIter = nav.Select("/carbonCopyOptions/srcDirs/dir");
                while (srcIter.MoveNext()) {
                    string pathString = srcIter.Current.Value;
                    string errorHolder;
                    DirectoryInfo fixedPath = null;

                    // Is path string valid?
                    if (!optFunc.CheckDirValidity(pathString, ref fixedPath, out errorHolder)) {
                        throw new Exception(errorHolder);
                    }
                    options.SourceDirs.Add(fixedPath);
                }

                // Dest dir
                XPathNodeIterator destIter = nav.Select("/carbonCopyOptions/destDir");
                if (!destIter.MoveNext()) {
                    throw new Exception("Couldn't find destDir configuration entry!");
                }
                string pathString2 = destIter.Current.GetAttribute("value", "");
                string errorHolder2;
                DirectoryInfo fixedPath2 = null;
                // Is path string valid?
                if (!optFunc.CheckDirValidity(pathString2, ref fixedPath2, out errorHolder2)) {
                    throw new Exception(errorHolder2);
                }
                options.DestDir = fixedPath2;

                // Finally, sanity check our populated CCO object
                string sanityErrors;
                if (!optFunc.SanityCheck(options, out sanityErrors)) {
                    throw new Exception("Error(s) found in backup profile file:\r\n" + sanityErrors);
                }
            }
            catch (Exception ex) {
                errorTxt = ex.Message;
                return false;
            }

            return true;
        }