Created
October 17, 2012 10:31
-
-
Save sharat/3904874 to your computer and use it in GitHub Desktop.
Get the operating system information using C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Management; | |
using System.Security.Principal; | |
using Microsoft.Win32; | |
public class SysInfoHelper | |
{ | |
public static void LogSystemInfo(int messageID) | |
{ | |
LogOSVersionInfo(messageID); | |
LogUserInfo(messageID); | |
LogSQLServerInfo(messageID); | |
} | |
private static void LogOSVersionInfo(int messageID) | |
{ | |
Logger.Write(Severity.Info, messageID, "--------------Operating System Info--------------"); | |
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_OperatingSystem"); | |
foreach (ManagementObject managementObject in mos.Get()) | |
{ | |
if (managementObject["Caption"] != null) | |
Console.WriteLine("\tOS Name : " + managementObject["Caption"].ToString()); //Display operating system caption | |
if (managementObject["OSArchitecture"] != null) | |
Console.WriteLine("\tArchitecture : " + managementObject["OSArchitecture"].ToString()); //Display operating system architecture. | |
if (managementObject["CSDVersion"] != null) | |
Console.WriteLine("\tService Pack : " + managementObject["CSDVersion"].ToString()); //Display operating system version. | |
} | |
getProcessorInfo(messageID); | |
Console.WriteLine("--------------Operating System Info--------------"); | |
} | |
private static void getProcessorInfo(int messageID) | |
{ | |
Console.WriteLine("\n\nDisplaying Processor Name...."); | |
RegistryKey processor_name = Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0", RegistryKeyPermissionCheck.ReadSubTree); //This registry entry contains entry for processor info. | |
if ((processor_name != null) && (processor_name.GetValue("ProcessorNameString") != null)) | |
Console.WriteLine("Processor " + processor_name.GetValue("ProcessorNameString")); //Display processor ingo. | |
} | |
private static void LogSQLServerInfo(int messageID) | |
{ | |
SqlDataSourceEnumerator dataSourceEnumarator = SqlDataSourceEnumerator.Instance; | |
DataTable dataTable = dataSourceEnumarator.GetDataSources(); | |
foreach (DataRow row in dataTable.Rows) | |
{ | |
Console.WriteLine("----------------------------SQL Source----------------------------"); | |
Console.WriteLine("Server Name:" + row["ServerName"]); | |
Console.WriteLine("Instance Name:" + row["InstanceName"]); | |
Console.WriteLine("Is Clustered:" + row["IsClustered"]); | |
Console.WriteLine("Version:" + row["Version"]); | |
Console.WriteLine("------------------------------------------------------------------"); | |
} | |
} | |
private static void LogUserInfo(int messageID) | |
{ | |
Console.WriteLine("\n----------------------------User Information----------------------------"); | |
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); | |
bool bIsAdministrator = principal.IsInRole(WindowsBuiltInRole.Administrator); | |
Console.WriteLine("User Name: WindowsIdentity.GetCurrent().Name"); | |
Console.WriteLine("Is Administrator? : " + (bIsAdministrator? "Yes" : "No")); | |
Console.WriteLine("--------------------------------------------------------------------------"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment