public bool writeString(string strData)
        {
            if (!_bFileOpen)             // open file
            {
                try
                {
                    _sw        = File.CreateText(_strFilename);
                    _bFileOpen = true;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString(), "Office Recovery Manager",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    _bFileOpen = false;
                }
            }

            if (!_bFileOpen)
            {
                return(false);
            }

            if (!_bPrototypeWritten)
            {
                _bPrototypeWritten = true;                 // make sure to set this flag to true before we call a reentrant method such like .WritePInstruction

                // write XML prototype, once and only once
                //
                int nOldIndent = getIndentLevel();
                setIndentLevel(0);

                xmlElement xml = new xmlElement("xml");
                xml.addAttrib("version", "1.0");
                xml.addAttrib("encoding", "UTF-8");
                xml.writePInstruction(this, 0);

                setIndentLevel(nOldIndent);
            }

            // actual write
            _sw.Write(strData);

            return(true);
        }
예제 #2
0
        bool saveAsXml(xmlWriter w, bool bFakedXml, string strKeyname, int hKey, string strLimitValue)
        {
            // write key name
            int      classLength          = 0;
            int      cSubKeys             = 0;              // number of subkeys
            int      cbMaxSubKey          = 0;              // longest subkey size
            int      cchMaxClass          = 0;              // longest class string
            int      cValues              = 0;              // number of values for key
            int      cchMaxValue          = 0;              // longest value name
            int      cbMaxValueData       = 0;              // longest value data
            int      cbSecurityDescriptor = 0;              // size of security descriptor
            FILETIME ftLastWriteTime      = new FILETIME(); // last write time

            int i;
            int retCode;


            // Get the class name and the value count.
            retCode = RegQueryInfoKeyA(hKey,                     // key handle
                                       null,                     // buffer for class name
                                       ref classLength,          // length of class string
                                       0,                        // reserved
                                       ref cSubKeys,             // number of subkeys
                                       ref cbMaxSubKey,          // longest subkey size
                                       ref cchMaxClass,          // longest class string
                                       ref cValues,              // number of values for this key
                                       ref cchMaxValue,          // longest value name
                                       ref cbMaxValueData,       // longest value data
                                       ref cbSecurityDescriptor, // security descriptor
                                       ref ftLastWriteTime);     // last write time


            if (retCode != ERROR_SUCCESS)
            {
                return(false);
            }

            // Enumerate the child keys, until RegEnumKeyEx fails.
            byte [] achKey = new byte[cbMaxSubKey + 1];

            xmlElement wkey = new xmlElement(XML_KEY);

            if (strKeyname.Length > 0)
            {
                if (!bFakedXml)                 // standard xml
                {
                    wkey.addAttrib(getEscapedXmlString(XML_NAME),
                                   getEscapedXmlString(strKeyname));

                    if (cSubKeys > 0 || cValues > 0)
                    {
                        wkey.write(w, 1, false, true);
                    }
                    else
                    {
                        wkey.writeEmpty(w, 1, false, true);
                    }
                }
                else                 // faked xml
                {
                    wkey.setName(getEscapedXmlString(strKeyname));
                }
            }


            // each 50 values, we pump a window message, to check out whether the user hit ESCAPE
            if ((_nSaveCounter++ % 50) == 0)
            {
                // this could be done....
            }

            // save values
            ArrayList arrValues = addRegistryValues(hKey);

            int nbItems = arrValues.Count;

            for (int j = 0; j < nbItems; j++)
            {
                keyValue p = (keyValue)arrValues[j];

                if (strLimitValue.Length != 0 && p.getName().CompareTo(strLimitValue) != 0)
                {
                    continue;
                }

                if (p.getName().CompareTo(IDS_DEFAULTVALUENAME) != 0 || p.getValue().CompareTo(IDS_DEFAULTVALUEVALUE) != 0)
                {
                    int dwType = p.getType();

                    if (!bFakedXml)                     // standard xml
                    {
                        xmlElement wvalue = new xmlElement(XML_VALUE);

                        wvalue.addAttrib(XML_NAME, getEscapedXmlString(p.getName()));
                        wvalue.addAttrib(XML_VALUE2, getEscapedXmlString(p.getValue()));

                        if (dwType != REG_SZ && dwType != REG_NONE)
                        {
                            wvalue.addAttrib(XML_TYPE, getEscapedXmlString(stringFromValueType(dwType)));
                        }

                        wvalue.writeEmpty(w, 1, false, true);
                    }
                    else                     // faked xml
                    {
                        wkey.addAttrib(getEscapedXmlString(p.getName()), getEscapedXmlString(p.getValue()));
                    }
                }
            }

            if (strKeyname.Length != 0)
            {
                if (bFakedXml)
                {
                    if (cSubKeys > 0)
                    {
                        wkey.write(w, 1, false, true);
                    }
                    else
                    {
                        wkey.writeEmpty(w, 1, false, true);
                    }
                }
            }

            for (i = 0, retCode = ERROR_SUCCESS; retCode == ERROR_SUCCESS; i++)
            {
                int achKeyMaxLength = cbMaxSubKey + 1;

                retCode = RegEnumKeyExA(hKey,
                                        i,
                                        ref achKey[0],
                                        ref achKeyMaxLength,
                                        0,
                                        null,
                                        ref cchMaxClass,
                                        ref ftLastWriteTime);

                if (retCode == ERROR_SUCCESS && achKeyMaxLength > 0)
                {
                    achKey[achKeyMaxLength] = 0;                   // force EOL

                    Encoding ascii = Encoding.ASCII;

                    // Convert the new byte[] into a char[] and then into a string.
                    char[] asciiChars = new char[ascii.GetCharCount(achKey, 0, achKeyMaxLength)];
                    ascii.GetChars(achKey, 0, achKeyMaxLength, asciiChars, 0);
                    string sKeyName = new string(asciiChars);

                    // open sub keys
                    int hSubkey = 0;
                    if (RegOpenKeyA(hKey, sKeyName, ref hSubkey) == ERROR_SUCCESS)
                    {
                        if (!saveAsXml(w, bFakedXml, sKeyName, hSubkey, strLimitValue))
                        {
                            return(false);
                        }

                        RegCloseKey(hSubkey);
                    }
                }
            }              // end for

            if (strKeyname.Length != 0)
            {
                if (!bFakedXml)
                {
                    if (cSubKeys > 0 || cValues > 0)
                    {
                        wkey.writeClosingTag(w, -1, false, true);
                    }
                }
                else
                {
                    // with faked xml, we only need to actually close the tag when there
                    // are keys under it, otherwise, we did a WriteEmpty above.
                    if (cSubKeys > 0)
                    {
                        wkey.writeClosingTag(w, -1, false, true);
                    }
                }
            }

            return(true);
        }
예제 #3
0
        public bool saveAsXml(xmlWriter w, bool bFakedXml, string strInPath, string strLimitValue)
        {
            string strPath = strInPath;

            if (strPath.Length == 0)
            {
                return(false);
            }

            string strMainKeyname = strPath;

            int nSlash = strPath.IndexOf("\\");

            if (nSlash > -1)
            {
                strMainKeyname = strPath.Substring(0, nSlash);
                strPath        = strPath.Substring(nSlash + 1);
            }
            else
            {
                strPath = "";
            }

            // open the key now
            RegistryKey reg = openKey(strMainKeyname, strPath);

            if (reg != null)           // it's ok
            {
                // write the main key here
                ArrayList arKeyPath = new ArrayList();

                int    nIndexSlash = 0, i;
                string strTmpPath = strInPath;

                do
                {
                    nIndexSlash = strTmpPath.IndexOf("\\", 0);
                    if (nIndexSlash > -1)
                    {
                        arKeyPath.Add(strTmpPath.Substring(0, nIndexSlash++));
                        strTmpPath = strTmpPath.Substring(nIndexSlash);
                    }
                    else
                    {
                        arKeyPath.Add(strTmpPath);
                    }
                }while (nIndexSlash > -1);

                int  nSize   = arKeyPath.Count;
                bool bResult = true;

                if (!bFakedXml)
                {
                    for (i = 0; i < nSize; i++)
                    {
                        xmlElement wkey = new xmlElement(XML_KEY);
                        wkey.addAttrib(getEscapedXmlString(XML_NAME), getEscapedXmlString((string)arKeyPath[i]));
                        wkey.write(w, 1, false, true);
                    }

                    bResult = saveAsXml(w, bFakedXml, "", getRegistryHandle(reg), strLimitValue);

                    for (i = 0; i < nSize; i++)
                    {
                        xmlElement wkey = new xmlElement(XML_KEY);
                        wkey.writeClosingTag(w, -1, false, true);
                    }
                }
                else
                {
                    nSize--;

                    for (i = 0; i < nSize; i++)
                    {
                        xmlElement wkey = new xmlElement((string)arKeyPath[i]);
                        wkey.write(w, 1, false, true);
                    }

                    bResult = saveAsXml(w, bFakedXml, (string)arKeyPath[nSize], getRegistryHandle(reg), strLimitValue);

                    for (i = 0; i < nSize; i++)
                    {
                        xmlElement wkey = new xmlElement((string)arKeyPath[nSize - 1 - i]);
                        wkey.writeClosingTag(w, -1, false, true);
                    }
                }

                reg.Close();

                return(bResult);
            }

            return(false);
        }