示例#1
0
        /// <summary>
        /// Returns all values from registry path, using specified Registry key
        /// </summary>
        /// <param name="path">path to registry key beginning </param>
        /// <returns>Dictionary w/ values or empty</returns>
        public static Dictionary <string, object> GetAllValues(RegistryKey rootKey, string path)
        {
            Dictionary <string, object> arValues = new Dictionary <string, object>();

            //KeyValuePair<string, object>[] arValues1 = null;
            string[] arKeys = null;
            //string subPath = "";
            RegistryKey key = null;

            //RegistryKey rootKey = Registry.CurrentUser;

            if (path == null)
            {
                Log4cs.Log("No path specified for Registry!", Importance.Error);
                return(arValues);
            }

            if (path.StartsWith("\\"))
            {
                path = path.Substring(1);
            }

            Log4cs.Log("Get values from: {0}\\{1}", rootKey.ToString(), path);

            try
            {
                key    = rootKey.OpenSubKey(path);
                arKeys = key.GetValueNames();
                Log4cs.Log(Importance.Debug, "Got " + arKeys.Length + " values in {0}\\{1}", rootKey.ToString(), path);

                if (arKeys.Length > 0)
                {
                    for (int i = 0; i < arKeys.Length; i++)
                    {
                        try
                        {
                            arValues[arKeys[i]] = key.GetValue(arKeys[i]).ToString();
                        } catch (Exception)
                        {
                            Log4cs.Log(Importance.Warning, "Duplicate key [" + arKeys[i] + "]");
                        }

                        //Log4cs.Log("\t" + arKeys[i] + "->" + key.GetValue( arKeys[i] ).ToString() );
                    }
                }  // END IF
            } catch (Exception ex)
            {
                Log4cs.Log(Importance.Error, "Error listing " + rootKey.ToString() + "\\" + path);
                Log4cs.Log(Importance.Debug, ex.ToString());
                //return m_arValues;
            }

            try
            {
                key.Close();
                rootKey.Close();
            } catch (Exception) { }

            return(arValues);
        }
示例#2
0
        public static Dictionary <string, Dictionary <string, object> > GetAllSubKeysValues(RegistryKey rootKey, string path)
        {
            Dictionary <string, Dictionary <string, object> > arDict = new Dictionary <string, Dictionary <string, object> >();
            Dictionary <string, object> arValues = new Dictionary <string, object>();
            //string[] arKeys = null;
            //string subPath = "";
            RegistryKey key = null;

            if (path == null)
            {
                Log4cs.Log("No path specified for Registry!", Importance.Error);
                return(null);
            }

            if (path.StartsWith("\\"))
            {
                path = path.Substring(1);
            }

            Log4cs.Log("Get subkeys from: {0}\\{1}", rootKey.ToString(), path);

            try
            {
                key = rootKey.OpenSubKey(path);
                string[] arSubKeys = key.GetSubKeyNames();
                for (int i = 0; (arSubKeys != null) && (i < arSubKeys.Length); i++)
                {
                    //Log4cs.Log(Importance.Debug, "\t" + arSubKeys[i]);
                    arDict[arSubKeys[i]] = RegHelper.GetAllValues(rootKey, path + "\\" + arSubKeys[i]);
                }
            } catch (Exception ex)
            {
                Log4cs.Log(Importance.Error, "Error listing " + rootKey.ToString() + "\\" + path);
                Log4cs.Log(Importance.Debug, ex.ToString());
                arDict = null;
            }

            try
            {
                key.Close();
                rootKey.Close();
            } catch (Exception) { }

            return(arDict);
        }
示例#3
0
        public static void Delete(RegistryKey rootKey, string path, string name)
        {
            RegistryKey key = null;

            try
            {
                //Log4cs.Log("Getting {0} from {1}", path + name, rootKey);
                key = rootKey.OpenSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                key.DeleteValue(name, true);
            } catch (Exception ex)
            {
                Log4cs.Log(Importance.Error, "Error deleting {0} in [{1}]{2}!", name, rootKey, path);
                Log4cs.Log(ex.ToString(), Importance.Debug);
            }

            try
            {
                key.Close();
                rootKey.Close();
            } catch (Exception) { }
        }
示例#4
0
        public static void Save(RegistryKey rootKey, string path, string name, int value)
        {
            RegistryKey key = null;
            object      obj = (object)value;

            try
            {
                //Log4cs.Log("Getting {0} from {1}", path + name, rootKey);
                key = rootKey.CreateSubKey(path, RegistryKeyPermissionCheck.ReadWriteSubTree);
                key.SetValue(name, value);
            } catch (Exception ex)
            {
                Log4cs.Log(Importance.Error, "Error saving {0} in [{1}]{2}!", name, rootKey, path);
                Log4cs.Log(ex.ToString(), Importance.Debug);
            }

            try
            {
                key.Close();
                rootKey.Close();
            } catch (Exception) { }
        }
示例#5
0
        /// <summary>
        /// Waits for any specified event
        /// </summary>
        /// <param name="arEvents">Events that could be</param>
        /// <param name="myEvents">One of events to wait</param>
        /// <param name="timeoutMs">Timeout</param>
        /// <returns>Id of event or WaitTimeout</returns>
        public static int WaitMyEvents(ref ManualResetEvent[] arEvents, int[] arEventsToWait, int timeoutMs, bool resetEvent)
        {
            int nEventId = WaitHandle.WaitTimeout;
            // How long we are waiting
            int      waitingTimeMs = 0;
            DateTime start;

            while (waitingTimeMs < timeoutMs)
            {
                start = DateTime.Now;
                //Log4cs.Log("Waiting for " + (timeoutMs - waitingTimeMs) + " ms.");
                nEventId = WaitHandle.WaitAny(arEvents, timeoutMs - waitingTimeMs, false);
                if (nEventId != WaitHandle.WaitTimeout)
                {
                    // Check if receive desired event
                    if (Array.IndexOf(arEventsToWait, nEventId) >= 0)
                    {
                        if (resetEvent)
                        {
                            Log4cs.Log(Importance.Debug, "Reseting event with ID: {0}", nEventId);
                            arEvents[nEventId].Reset();
                        }
                        return(nEventId);
                    }
                    else
                    {
                        waitingTimeMs += ((TimeSpan)(DateTime.Now - start)).Milliseconds;
                    }
                }
                else
                {
                    // Stop waiting and return
                    waitingTimeMs = timeoutMs;
                }
            }  // END WHILE

            return(nEventId);
        }
示例#6
0
        public static object GetValue(RegistryKey rootKey, string path, string name)
        {
            RegistryKey key   = null;
            object      value = "";

            try
            {
                //Log4cs.Log("Getting {0} from {1}", path + name, rootKey);
                key   = rootKey.OpenSubKey(path);
                value = key.GetValue(name);
            } catch (Exception ex)
            {
                Log4cs.Log("Error getting " + name + " from " + path, Importance.Error);
                Log4cs.Log(ex.ToString(), Importance.Debug);
            }

            try
            {
                key.Close();
                rootKey.Close();
            } catch (Exception) { /* Log4cs.Log(ex.ToString()); */ }

            return(value);
        }