Tag: get

C#

Get local system users

Use the following code to retrieve local system users and some of their properties, using WMI and Management namespace . Method public List<User> GetUsers() { try { List<User> users = new List<User>(); foreach (ManagementObject mo in (new ManagementObjectSearcher(new SelectQuery("Win32_UserAccount"))).Get()) { users.Add(new User { Name = mo["Name"].ToString(), Caption = mo["Caption"].ToString(), IsDisabled = Boolean.Parse(mo["Disabled"].ToString()), IsLocalAccount = Boolean.Parse(mo["LocalAccount"].ToString()), …

C#

Get public/external IP address

Use the following code to get your wan/external/public ip address using http://dyndns.org site. try { System.Net.WebClient wc = new System.Net.WebClient(); String s = wc.DownloadString("http://checkip.dyndns.org"); s = ( new System.Text.RegularExpressions.Regex( @"bd{1,3}.d{1,3}.d{1,3}.d{1,3}b")). Match(s).Value; wc.Dispose(); Console.WriteLine(s); } catch (Exception) { //… }try { System.Net.WebClient wc = new System.Net.WebClient(); String s = wc.DownloadString("http://checkip.dyndns.org"); s = ( new System.Text.RegularExpressions.Regex( @"bd{1,3}.d{1,3}.d{1,3}.d{1,3}b")). …

C#

Get antispyware name in Windows using C#

To detect installed anti-spyware’s name in Windows we need ManagementObjectSearcher class. To use it, we have to add a reference to the namespace System.Management. try { ManagementObjectSearcher mos = null; if (Environment.OSVersion.Version.Major > 5) { mos = new ManagementObjectSearcher(@"\" + Environment.MachineName + @"rootSecurityCenter2", "SELECT * FROM AntiSpywareProduct"); } else { mos = new ManagementObjectSearcher(@"\" + …

C#

Get firewall name in Windows using C#

To detect installed firewall’s name in Windows we need ManagementObjectSearcher class. To use it, we have to add a reference to the namespace System.Management. try { ManagementObjectSearcher mos = null; if (Environment.OSVersion.Version.Major > 5) { mos = new ManagementObjectSearcher(@"\" + Environment.MachineName + @"rootSecurityCenter2", "SELECT * FROM FirewallProduct"); } else { mos = new ManagementObjectSearcher(@"\" + …

C#

Get antivirus name on Windows using C#

To detect antivirus name in Windows we need ManagementObjectSearcher class. To use it, we have to add reference in the project to the namespace System.Management. try { ManagementObjectSearcher mos = null; //Windows Vista/7/8 if (Environment.OSVersion.Version.Major > 5) { mos = new ManagementObjectSearcher(@"\" + Environment.MachineName + @"rootSecurityCenter2", "SELECT * FROM AntivirusProduct"); } //Windows XP else { …

C#

List all subfolders in C#

Use the following code to list all subdirectories of a folder. try { String[] allfolders = System.IO.Directory.GetDirectories("C:\", "*", System.IO.SearchOption.TopDirectoryOnly); if (allfolders.Length == 0) Console.WriteLine("empty"); else foreach (String folder in allfolders) Console.WriteLine(folder.Substring(folder.LastIndexOf("\") + 1) ); } catch (Exception) { //… }try { String[] allfolders = System.IO.Directory.GetDirectories("C:\", "*", System.IO.SearchOption.TopDirectoryOnly); if (allfolders.Length == 0) Console.WriteLine("empty"); else foreach (String …

C#

Get system hard drives with C#

Retrieve the available disk drives with GetLogicalDrives function. try { foreach (String lodi in Environment.GetLogicalDrives()) if (lodi != String.Empty) Console.WriteLine(lodi); } catch (Exception) { //… }try { foreach (String lodi in Environment.GetLogicalDrives()) if (lodi != String.Empty) Console.WriteLine(lodi); } catch (Exception) { //… }

C#

Get system services with C#

To list system services, we are going to use ServiceController class. Add a reference to System.ServiceProcess namespace first. try { foreach (System.ServiceProcess.ServiceController s in System.ServiceProcess.ServiceController.GetServices()) Console.WriteLine(s.Status + " — " + s.ServiceName); } catch (Exception) { //… }try { foreach (System.ServiceProcess.ServiceController s in System.ServiceProcess.ServiceController.GetServices()) Console.WriteLine(s.Status + " — " + s.ServiceName); } catch (Exception) { …

C#

Get all opened windows in C#

With the following code you can get all opened windows for the current user. public void GetOpenWindows() { IntPtr hwnd; try { foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses()) if ((hwnd = proc.MainWindowHandle) != IntPtr.Zero) Console.WriteLine(hwnd.ToString() + " " + proc.MainWindowTitle); } catch (Exception) { } }public void GetOpenWindows() { IntPtr hwnd; try { foreach (System.Diagnostics.Process proc …