示例#1
0
        public void CopyTo(Array array, int index)
        {
            if (this.isDisposed)
            {
                throw new ObjectDisposedException(name);
            }
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            if ((index < array.GetLowerBound(0)) || (index > array.GetUpperBound(0)))
            {
                throw new ArgumentOutOfRangeException("index");
            }
            int       num  = array.Length - index;
            int       num2 = 0;
            ArrayList list = new ArrayList();
            ManagementObjectEnumerator enumerator = this.GetEnumerator();

            while (enumerator.MoveNext())
            {
                ManagementBaseObject current = enumerator.Current;
                list.Add(current);
                num2++;
                if (num2 > num)
                {
                    throw new ArgumentException(null, "index");
                }
            }
            list.CopyTo(array, index);
        }
示例#2
0
        static void Main(string[] args)
        {
            RegistryKey registry = null;

            try
            {
                registry = Registry.CurrentUser.OpenSubKey("Software", true);
                registry = registry.CreateSubKey("Storozhuk");
                registry.SetValue("username", SystemInformation.UserName);
                registry.SetValue("computer_name", SystemInformation.ComputerName);
                registry.SetValue("root_cat", Environment.SystemDirectory);
                registry.SetValue("mouse_buttons", SystemInformation.MouseButtons);
                registry.SetValue("monitor_height", SystemInformation.PrimaryMonitorSize.Height);
                ManagementObjectSearcher   moSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
                ManagementObjectEnumerator enumerator = moSearcher.Get().GetEnumerator();
                enumerator.MoveNext();
                registry.SetValue("hdd_serial_number", enumerator.Current["SerialNumber"].ToString());
                int hddCount = 1;
                while (enumerator.MoveNext())
                {
                    hddCount++;
                }
                registry.SetValue("hdd_count", hddCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show("exc");
            }
            finally
            {
                registry.Close();
            }
        }
示例#3
0
        public static string GetMachinePrincipalName()
        {
            string Result = ".";

            try
            {
                ManagementScope localWMI = new ManagementScope(ManagementPath.DefaultPath);
                localWMI.Connect();
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
                ManagementObjectSearcher   computerSystemSearcher = null;
                ManagementObjectCollection computerSystemList     = null;
                ManagementObjectEnumerator computerSystemEnum     = null;
                try
                {
                    computerSystemSearcher = new ManagementObjectSearcher(localWMI, query);
                    computerSystemList     = computerSystemSearcher.Get();
                    computerSystemEnum     = computerSystemList.GetEnumerator();
                    if (computerSystemEnum.MoveNext())
                    {
                        var computerSystem = computerSystemEnum.Current;
                        if ((bool)computerSystem["PartOfDomain"])
                        {
                            Result = computerSystem["DNSHostName"].ToString() + "." + computerSystem["Domain"].ToString();
                        }
                        else
                        {
                            Result = computerSystem["DNSHostName"].ToString();
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException("No instances of Win32_ComputerSystem WMI class returned.");
                    }
                }
                finally
                {
                    if (computerSystemEnum != null)
                    {
                        computerSystemEnum.Dispose();
                    }
                    if (computerSystemList != null)
                    {
                        computerSystemList.Dispose();
                    }
                    if (computerSystemSearcher != null)
                    {
                        computerSystemSearcher.Dispose();
                    }
                }
            }
            catch
            {
                Result = Environment.MachineName;
            }
            return(Result);
        }
示例#4
0
        /// <summary>
        /// Procedimento principal
        /// </summary>
        private void MainProc()
        {
            while (_run)
            {
                using (ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT name, processId, workingSetSize FROM win32_process"))
                    using (ManagementObjectCollection processes = query.Get())
                    {
                        DetectedProcessesCount = processes.Count;
                        ManagementObjectEnumerator enumerator = processes.GetEnumerator();

                        // Procura por processos com uso de memória alarmante
                        int  pid = 0, workingSet = 0;
                        bool wellFuck = false;
                        while (!wellFuck && enumerator.MoveNext())
                        {
                            string name = enumerator.Current["name"].ToString();
                            if (!name.Equals(ProcessName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                continue;
                            }

                            pid        = int.Parse(enumerator.Current["processId"].ToString());
                            workingSet = int.Parse(enumerator.Current["workingSetSize"].ToString());

                            if (workingSet >= MemoryUsageLimit * 1024)
                            {
                                wellFuck = true;
                            }
                        }

                        // Se não estiver em estado de alerta e houver algum processo fora do comum,
                        // lança o evento de aviso
                        if (wellFuck && !_inDanger)
                        {
                            _inDanger = true;
                            OnDanger(new Process()
                            {
                                ProcessId = pid, WorkingSet = workingSet
                            }, EventArgs.Empty);
                        }

                        // Se estiver em estado de alerta e tudo estiver normal, lança o evento de
                        // notificação de segurança
                        else if (!wellFuck && _inDanger)
                        {
                            _inDanger = false;
                            OnSafe(null, EventArgs.Empty);
                        }
                    }

                Thread.Sleep(Interval);
            }
        }
        /// <overload>
        ///    Copies the collection to an array.
        /// </overload>
        /// <summary>
        ///    <para> Copies the collection to an array.</para>
        /// </summary>
        /// <param name='array'>An array to copy to. </param>
        /// <param name='index'>The index to start from. </param>
        public void CopyTo(Array array, int index)
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException(name);
            }

            if (null == array)
            {
                throw new ArgumentNullException(nameof(array));
            }

            if ((index < array.GetLowerBound(0)) || (index > array.GetUpperBound(0)))
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            // Since we don't know the size until we've enumerated
            // we'll have to dump the objects in a list first then
            // try to copy them in.

            int       capacity   = array.Length - index;
            int       numObjects = 0;
            ArrayList arrList    = new ArrayList();

            ManagementObjectEnumerator en = this.GetEnumerator();
            ManagementBaseObject       obj;

            while (en.MoveNext())
            {
                obj = en.Current;

                arrList.Add(obj);
                numObjects++;

                if (numObjects > capacity)
                {
                    throw new ArgumentException(null, nameof(index));
                }
            }

            // If we get here we are OK. Now copy the list to the array
            arrList.CopyTo(array, index);

            return;
        }
示例#6
0
        public static ManagementObject Query(string scope, string wmiClass)
        {
            try
            {
                using (var searcher = new ManagementObjectSearcher($"{scope}", $"SELECT * FROM {wmiClass}"))
                {
                    ManagementObjectEnumerator enumerator = searcher.Get().GetEnumerator();
                    if (enumerator.MoveNext())
                    {
                        return(enumerator.Current as ManagementObject);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(null);
        }
示例#7
0
        private static string GetParentProcessName()
        {
            using (Process process = Process.GetCurrentProcess())
            {
                string query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", process.Id);
                ManagementObjectSearcher search = new ManagementObjectSearcher("root\\CIMV2", query);

                ManagementObjectEnumerator enumerator = search.Get().GetEnumerator();
                enumerator.MoveNext();
                ManagementBaseObject result = enumerator.Current;

                try
                {
                    using (Process parentProcess = Process.GetProcessById((int)(uint)result["ParentProcessId"]))
                    {
                        return(parentProcess.ProcessName);
                    }
                }
                catch (ArgumentException e)
                {
                    return("#unknown#");
                }
            }
        }
示例#8
0
 private void UpdateDeviceList()
 {
     devices             = ManagementObjectEnumerator.Enumerate <Device>(filter, DeviceFactory.CreateDevice).ToList();
     lastUpdateTimestamp = DateTime.Now;
 }