示例#1
0
        internal System.Collections.Generic.SortedList <string, string> EnumKeys(string p_SubKeyName)
        {
            // Return a sorted list of subkeys
            Generic.SortedList <string, string> Values;
            Generic.SortedList <string, string> RetValues = new Generic.SortedList <string, string>();
            string[] Directories;
            string   DefaultValue;

            try
            {
                GetProfileMutex("EnumKeys", p_SubKeyName);
                sw.Reset(); sw.Start(); // Start timing this call
                TL.LogMessage("EnumKeys", "SubKey: \"" + p_SubKeyName + "\"");

                Directories = FileStore.GetDirectoryNames(p_SubKeyName);            // Get a list of the keys
                foreach (string Directory in Directories)                           // Process each key in trun
                {
                    try                                                             // If there is an error reading the data don't include in the returned list
                    {
                        Values       = ReadValues(p_SubKeyName + @"\" + Directory); // Read the values of this key to find the default value
                        DefaultValue = Values.Item(COLLECTION_DEFAULT_VALUE_NAME);  // Save the default value
                        if (DefaultValue == COLLECTION_DEFAULT_UNSET_VALUE)
                        {
                            DefaultValue = "";
                        }
                        RetValues.Add(Directory, DefaultValue); // Add the directory name and default value to the hashtable
                    }
                    catch
                    {
                    }
                    Values = null /* TODO Change to default(_) if this is not a reference type */;
                }
                sw.Stop(); TL.LogMessage("  ElapsedTime", "  " + sw.ElapsedMilliseconds + " milliseconds");
            }
            finally
            {
                ProfileMutex.ReleaseMutex();
            }
            return(RetValues);
        }
示例#2
0
        internal Generic.SortedList <string, string> EnumProfile(string p_SubKeyName)
        {
            // Returns a sorted list of key values
            Generic.SortedList <string, string> Values;
            Generic.SortedList <string, string> RetValues = new Generic.SortedList <string, string>();

            try
            {
                GetProfileMutex("EnumProfile", p_SubKeyName);
                sw.Reset(); sw.Start(); // Start timing this call
                TL.LogMessage("EnumProfile", "SubKey: \"" + p_SubKeyName + "\"");

                Values = ReadValues(p_SubKeyName);                            // Read values from profile XML file
                foreach (Generic.KeyValuePair <string, string> kvp in Values) // Retrieve each key/value  pair in turn
                {
                    if (kvp.Key == COLLECTION_DEFAULT_VALUE_NAME)
                    {
                        if (kvp.Value == COLLECTION_DEFAULT_UNSET_VALUE)
                        {
                        }
                        else
                        {
                            RetValues.Add("", kvp.Value);// Add any other value to the return value
                        }
                    }
                    else
                    {
                        RetValues.Add(kvp.Key, kvp.Value);
                    }
                }
                Values = null /* TODO Change to default(_) if this is not a reference type */;
                sw.Stop(); TL.LogMessage("  ElapsedTime", "  " + sw.ElapsedMilliseconds + " milliseconds");
            }
            finally
            {
                ProfileMutex.ReleaseMutex();
            }
            return(RetValues);
        }
示例#3
0
        private void MigrateKey(RegistryKey p_FromKey, string p_ToDir)
        {
            // Subroutine used for one off copy of registry profile to new XML profile
            string[]    ValueNames, SubKeyNames;
            RegistryKey FromKey;

            Generic.SortedList <string, string> Values = new Generic.SortedList <string, string>();
            // Recusively copy contents from one key to the other
            Stopwatch swLocal;

            ;                  /* Cannot convert LocalDeclarationStatementSyntax, System.NotSupportedException: StaticKeyword not supported!
                                * at ICSharpCode.CodeConverter.CSharp.SyntaxKindExtensions.ConvertToken(SyntaxKind t, TokenContext context)
                                * at ICSharpCode.CodeConverter.CSharp.CommonConversions.ConvertModifier(SyntaxToken m, TokenContext context)
                                * at ICSharpCode.CodeConverter.CSharp.CommonConversions.<ConvertModifiersCore>d__15.MoveNext()
                                * at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
                                * at Microsoft.CodeAnalysis.SyntaxTokenList.CreateNode(IEnumerable`1 tokens)
                                * at ICSharpCode.CodeConverter.CSharp.CommonConversions.ConvertModifiers(IEnumerable`1 modifiers, TokenContext context, Boolean isVariableOrConst)
                                * at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.MethodBodyVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
                                * at Microsoft.CodeAnalysis.VisualBasic.Syntax.LocalDeclarationStatementSyntax.Accept[TResult](VisualBasicSyntaxVisitor`1 visitor)
                                * at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.Visit(SyntaxNode node)
                                * at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node)
                                * at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
                                *
                                * Input:
                                *
                                * Static RecurseDepth As Integer
                                *
                                */
            RecurseDepth += 1; // Increment the recursion depth indicator

            swLocal = Stopwatch.StartNew();
            TL.LogMessage("MigrateKeys " + RecurseDepth.ToString(), "To Directory: " + p_ToDir);
            try
            {
                TL.LogMessage("MigrateKeys" + RecurseDepth.ToString(), "From Key: " + p_FromKey.Name + ", SubKeyCount: " + p_FromKey.SubKeyCount.ToString() + ", ValueCount: " + p_FromKey.ValueCount.ToString());
            }
            catch (Exception ex)
            {
                TL.LogMessage("MigrateKeys", "Exception processing \"" + p_ToDir + "\": " + ex.ToString());
                TL.LogMessage("MigrateKeys", "Exception above: no action taken, continuing...");
            }

            // First copy values from the from key to the to key
            ValueNames = p_FromKey.GetValueNames();
            Values.Add(COLLECTION_DEFAULT_VALUE_NAME, COLLECTION_DEFAULT_UNSET_VALUE);
            foreach (string ValueName in ValueNames)
            {
                if (ValueName == "")
                {
                    Values.Remove(COLLECTION_DEFAULT_VALUE_NAME); // Remove the default unset value and replace with actual value
                    Values.Add(COLLECTION_DEFAULT_VALUE_NAME, p_FromKey.GetValue(ValueName).ToString());
                }
                else
                {
                    Values.Add(ValueName, p_FromKey.GetValue(ValueName).ToString());
                }
            }
            WriteValues(p_ToDir, ref Values); // Write values to XML file

            // Now process the keys
            SubKeyNames = p_FromKey.GetSubKeyNames();
            foreach (string SubKeyName in SubKeyNames)
            {
                FromKey = p_FromKey.OpenSubKey(SubKeyName);       // Point at the source to copy to it
                CreateKey(p_ToDir + @"\" + SubKeyName);
                MigrateKey(FromKey, p_ToDir + @"\" + SubKeyName); // Recursively process each key
                FromKey.Close();
            }
            swLocal.Stop(); TL.LogMessage("  ElapsedTime " + RecurseDepth.ToString(), "  " + swLocal.ElapsedMilliseconds + " milliseconds, Completed Directory: " + p_ToDir);
            RecurseDepth -= 1; // Decrement the recursion depth counter
            swLocal       = null;
        }
示例#4
0
        private new void WriteValues(string p_SubKeyName, ref Generic.SortedList <string, string> p_KeyValuePairs, bool p_CheckForCurrentProfileStore)
        {
            // Write  all key values to an XML file
            // SubKey has to be absolute from the profile store root
            XmlWriterSettings WriterSettings;
            string            FName;
            int Ct;

            swSupport.Reset(); swSupport.Start(); // Start timing this call
            TL.LogMessage("  WriteValues", "  SubKeyName: " + p_SubKeyName);
            if (Strings.Left(p_SubKeyName, 1) != @"\")
            {
                p_SubKeyName = @"\" + p_SubKeyName;
            }

            try
            {
                Ct = 0;
                foreach (Generic.KeyValuePair <string, string> kvp in p_KeyValuePairs)
                {
                    Ct += 1;
                    TL.LogMessage("  WriteValues List", "  " + Ct.ToString() + " " + kvp.Key + " = " + kvp.Value);
                }

                WriterSettings        = new XmlWriterSettings();
                WriterSettings.Indent = true;
                FName = FileStore.FullPath(p_SubKeyName + @"\" + VALUES_FILENAME_NEW);
                XmlWriter  Writer;
                FileStream FStream;
                FStream = new FileStream(FName, FileMode.Create, FileAccess.Write, FileShare.None, 2048, FileOptions.WriteThrough);
                Writer  = XmlWriter.Create(FStream, WriterSettings);
                // Writer = XmlWriter.Create(FName, WriterSettings)
                using (Writer)
                {
                    Writer.WriteStartDocument();
                    Writer.WriteStartElement(PROFILE_NAME);                                                                 // Write the profile element
                    Writer.WriteStartElement(DEFAULT_ELEMENT_NAME);                                                         // Write the default element
                    Writer.WriteAttributeString(VALUE_ATTRIBUTE_NAME, p_KeyValuePairs.Item(COLLECTION_DEFAULT_VALUE_NAME)); // Write the default value
                    Writer.WriteEndElement();
                    Ct = 0;
                    foreach (Generic.KeyValuePair <string, string> kvp in p_KeyValuePairs) // Write each named value in turn
                    {
                        Ct += 1;
                        TL.LogMessage("  Writing Value", "  " + Ct.ToString() + " " + kvp.Key + " = " + kvp.Value);
                        if (kvp.Value == null)
                        {
                            TL.LogMessage("  Writing Value", "  WARNING - Suppplied Value is Nothing not empty string");
                        }
                        switch (kvp.Key)
                        {
                        case object _ when COLLECTION_DEFAULT_VALUE_NAME     // Ignore the default value entry
                            :
                        {
                            break;
                        }

                        default:
                        {
                            Writer.WriteStartElement(VALUE_ELEMENT_NAME);                 // Write the element name
                            Writer.WriteAttributeString(NAME_ATTRIBUTE_NAME, kvp.Key);    // Write the name attribute
                            Writer.WriteAttributeString(VALUE_ATTRIBUTE_NAME, kvp.Value); // Write the value attribute
                            Writer.WriteEndElement();                                     // Close this element
                            break;
                        }
                        }
                    }
                    Writer.WriteEndElement();

                    // Flush and close the writer object to complete writing of the XML file.
                    Writer.Close(); // Actualy write the XML to a file
                }
                try
                {
                    FStream.Flush();
                    FStream.Close();
                    FStream.Dispose();
                    FStream = null;
                }
                catch (Exception ex)
                {
                }// Ensure no error occur from this tidying up

                Writer = null;
                try // New file successfully created so now rename the current file to original and rename the new file to current
                {
                    if (p_CheckForCurrentProfileStore)
                    {
                        FileStore.Rename(p_SubKeyName + @"\" + VALUES_FILENAME, p_SubKeyName + @"\" + VALUES_FILENAME_ORIGINAL);
                    }
                    try
                    {
                        FileStore.Rename(p_SubKeyName + @"\" + VALUES_FILENAME_NEW, p_SubKeyName + @"\" + VALUES_FILENAME);
                    }
                    catch (Exception ex2)
                    {
                        // Attempt to rename new file as current failed so try and restore the original file
                        TL.Enabled = true;
                        TL.LogMessage("XMLAccess:WriteValues", "Unable to rename new profile file to current - " + p_SubKeyName + @"\" + VALUES_FILENAME_NEW + "to " + p_SubKeyName + @"\" + VALUES_FILENAME + " " + ex2.ToString());
                        try
                        {
                            FileStore.Rename(p_SubKeyName + @"\" + VALUES_FILENAME_ORIGINAL, p_SubKeyName + @"\" + VALUES_FILENAME);
                        }
                        catch (Exception ex3)
                        {
                            // Restoration also failed so no clear recovery from this point
                            TL.Enabled = true;
                            TL.LogMessage("XMLAccess:WriteValues", "Unable to rename original profile file to current - " + p_SubKeyName + @"\" + VALUES_FILENAME_ORIGINAL + "to " + p_SubKeyName + @"\" + VALUES_FILENAME + " " + ex3.ToString());
                        }
                    }
                }
                catch (Exception ex1)
                {
                    // No clear remedial action as the current file rename failed so just leave as is
                    TL.Enabled = true;
                    TL.LogMessage("XMLAccess:WriteValues", "Unable to rename current profile file to original - " + p_SubKeyName + @"\" + VALUES_FILENAME + "to " + p_SubKeyName + @"\" + VALUES_FILENAME_ORIGINAL + " " + ex1.ToString());
                }

                WriterSettings = null;

                swSupport.Stop();
                TL.LogMessage("  WriteValues", "  Created cache entry " + p_SubKeyName + " - " + swSupport.ElapsedMilliseconds + " milliseconds");
            }
            catch (Exception ex)
            {
                TL.LogMessageCrLf("  WriteValues", "  Exception " + p_SubKeyName + " " + ex.ToString());
                Interaction.MsgBox("XMLAccess:Writevalues " + p_SubKeyName + " " + ex.ToString());
            }
        }
示例#5
0
 private new void WriteValues(string p_SubKeyName, ref Generic.SortedList <string, string> p_KeyValuePairs)
 {
     // Make the general case check for existence of a current Profile.xml file. Most cases need this
     // The exception is the CreateKey where the Profile.xmldefinitlkey won't exist as we are about to create it for the first time
     WriteValues(p_SubKeyName, ref p_KeyValuePairs, true);
 }
示例#6
0
        private Generic.SortedList <string, string> ReadValues(string p_SubKeyName)
        {
            Generic.SortedList <string, string> Retval = new Generic.SortedList <string, string>();
            // Read all values in a key - SubKey has to be absolute from the profile store root
            XmlReaderSettings ReaderSettings;
            string            LastElementName = "";
            string            NextName        = "";
            string            ValueName       = "";
            bool   ReadOK = false;
            int    RetryCount;
            bool   ErrorOccurred = false;
            string ValuesFileName; // Name of the profile file from which to read
            bool   ExistsValues, ExistsValuesOriginal, ExistsValuesNew;

            swSupport.Reset(); swSupport.Start(); // Start timing this call
            if (Strings.Left(p_SubKeyName, 1) != @"\")
            {
                p_SubKeyName = @"\" + p_SubKeyName; // Condition to have leading \
            }
            TL.LogMessage("  ReadValues", "  SubKeyName: " + p_SubKeyName);

            ValuesFileName = VALUES_FILENAME; // Initialise to the file holding current values
            RetryCount     = -1;              // Initialise to ensure we get RETRY_Max number of retrys

            // Determine what files exist and handle the case where this key has not yet been created
            ExistsValues         = FileStore.Exists(p_SubKeyName + @"\" + VALUES_FILENAME);
            ExistsValuesOriginal = FileStore.Exists(p_SubKeyName + @"\" + VALUES_FILENAME_ORIGINAL);
            ExistsValuesNew      = FileStore.Exists(p_SubKeyName + @"\" + VALUES_FILENAME_NEW);
            if (!ExistsValues & !ExistsValuesOriginal)
            {
                throw new ProfileNotFoundException("No profile files exist for this key: " + p_SubKeyName);
            }
            do
            {
                RetryCount += 1;
                try
                {
                    ReaderSettings = new XmlReaderSettings();
                    ReaderSettings.IgnoreWhitespace = true;
                    using (XmlReader Reader = XmlReader.Create(FileStore.FullPath(p_SubKeyName + @"\" + ValuesFileName), ReaderSettings))
                    {
                        Reader.Read();
                        Reader.Read();

                        // Start reading profile strings
                        while (Reader.Read())
                        {
                            switch (Reader.NodeType)
                            {
                            case XmlNodeType.Element:
                            {
                                switch (Reader.Name)
                                {
                                case object _ when DEFAULT_ELEMENT_NAME:
                                {
                                    Retval.Add(COLLECTION_DEFAULT_VALUE_NAME, Reader.GetAttribute(VALUE_ATTRIBUTE_NAME));
                                    TL.LogMessage("    ReadValues", "    found " + COLLECTION_DEFAULT_VALUE_NAME + " = " + Retval.Item(COLLECTION_DEFAULT_VALUE_NAME));
                                    break;
                                }

                                case object _ when VALUE_ELEMENT_NAME:
                                {
                                    ValueName = Reader.GetAttribute(NAME_ATTRIBUTE_NAME);
                                    Retval.Add(ValueName, Reader.GetAttribute(VALUE_ATTRIBUTE_NAME));
                                    TL.LogMessage("    ReadValues", "    found " + ValueName + " = " + Retval.Item(ValueName));
                                    break;
                                }

                                default:
                                {
                                    TL.LogMessage("    ReadValues", "    ## Found unexpected Reader.Name: " + Reader.Name.ToString());
                                    break;
                                }
                                }

                                break;
                            }

                            default:
                            {
                                break;
                            }
                            }
                        }

                        Reader.Close();
                    }

                    swSupport.Stop();
                    TL.LogMessage("  ReadValues", "  added to cache - " + swSupport.ElapsedMilliseconds + " milliseconds");
                    ReadOK = true;
                }
                catch (Exception ex)
                {
                    ErrorOccurred = true;
                    if (RetryCount == RETRY_MAX)
                    {
                        if (ValuesFileName == VALUES_FILENAME)
                        {
                            ValuesFileName = VALUES_FILENAME_ORIGINAL;
                            RetryCount     = -1;
                            LogEvent("XMLAccess:ReadValues", "Error reading profile on final retry - attempting recovery from previous version", EventLogEntryType.Warning, EventLogErrors.XMLAccessRecoveryPreviousVersion, ex.ToString());
                            TL.LogMessageCrLf("  ReadValues", "Final retry exception - attempting recovery from previous version: " + ex.ToString());
                        }
                        else
                        {
                            LogEvent("XMLAccess:ReadValues", "Error reading profile on final retry", EventLogEntryType.Error, EventLogErrors.XMLAccessReadError, ex.ToString());
                            TL.LogMessageCrLf("  ReadValues", "Final retry exception: " + ex.ToString());
                            throw new ProfilePersistenceException("XMLAccess Exception", ex);
                        }
                    }
                    else
                    {
                        LogEvent("XMLAccess:ReadValues", "Error reading profile - retry: " + RetryCount, EventLogEntryType.Warning, EventLogErrors.XMLAccessRecoveryPreviousVersion, ex.Message);
                        TL.LogMessageCrLf("  ReadValues", "Retry " + RetryCount + " exception: " + ex.ToString());
                    }
                }

                if (ErrorOccurred)
                {
                    System.Threading.Thread.Sleep(RETRY_INTERVAL);
                }
            }while (!ReadOK)// Get rid of the XML version string// Read in the Profile name tag// Found default value// Fount an element name// Close the IO readers// Set the exit flag here when a read has been successful // Wait if an error occurred
            ;
            if (ErrorOccurred)
            {
                LogEvent("XMLAccess:ReadValues", "Recovered from read error OK", EventLogEntryType.SuccessAudit, EventLogErrors.XMLAccessRecoveredOK, null /* TODO Change to default(_) if this is not a reference type */);
                TL.LogMessage("  ReadValues", "Recovered from read error OK");
            }

            return(Retval);
        }
示例#7
0
        internal void CreateKey(string p_SubKeyName)
        {
            // Create a key
            Generic.SortedList <string, string> InitalValues = new Generic.SortedList <string, string>();
            string[] SubKeys;
            string   SubKey;
            int      i, j;

            try
            {
                GetProfileMutex("CreateKey", p_SubKeyName);
                sw.Reset(); sw.Start(); // Start timing this call
                TL.LogMessage("CreateKey", "SubKey: \"" + p_SubKeyName + "\"");

                p_SubKeyName = Strings.Trim(p_SubKeyName);                                                           // Normalise the string:
                SubKeys      = Strings.Split(p_SubKeyName, @"\", Compare: Microsoft.VisualBasic.CompareMethod.Text); // Parse p_SubKeyName into its elements
                switch (p_SubKeyName)
                {
                case ""      // Null path so do nothing
                    :
                {
                    break;
                }

                case @"\"     // Root node so just create this
                    :
                {
                    if (!FileStore.Exists(@"\" + VALUES_FILENAME))
                    {
                        TL.LogMessage("  CreateKey", @"  Creating root key ""\""");
                        InitalValues.Clear();                       // Now add the file containing the contents of the key
                        InitalValues.Add(COLLECTION_DEFAULT_VALUE_NAME, COLLECTION_DEFAULT_UNSET_VALUE);
                        WriteValues(@"\", ref InitalValues, false); // Write the profile file, don't check if it already exists
                    }
                    else
                    {
                        TL.LogMessage("  CreateKey", "  Root key alread exists");
                    }
                    break;
                }

                default:
                {
                    for (i = 0; i <= SubKeys.Length - 1; i++)
                    {
                        SubKey = "";
                        for (j = 0; j <= i; j++)
                        {
                            SubKey = SubKey + @"\" + SubKeys[j];
                        }
                        if (!FileStore.Exists(SubKey + @"\" + VALUES_FILENAME))
                        {
                            FileStore.CreateDirectory(SubKey, TL);        // It doesn't exist so create it
                            InitalValues.Clear();                         // Now add the file containing the contents of the key
                            InitalValues.Add(COLLECTION_DEFAULT_VALUE_NAME, COLLECTION_DEFAULT_UNSET_VALUE);
                            WriteValues(SubKey, ref InitalValues, false); // Write the profile file
                        }
                    }

                    break;
                }
                }
                sw.Stop(); TL.LogMessage("  ElapsedTime", "  " + sw.ElapsedMilliseconds + " milliseconds");
            }
            finally
            {
                ProfileMutex.ReleaseMutex();
            }
        }