Showing posts with label C# WMI. Show all posts
Showing posts with label C# WMI. Show all posts

11 December 2008

C# Read Harddisk Serial Number WMI ManagementClass Win32_LogicalDisk

With the Win32_LogicalDisk Management path we can reach the local disk information.

Even you can read harddisk serial number,
size, partitions, free space and so on.

Now let's read the serial number of a harddisk in C# with ManagementClass WMI.
public static string HardDiskID()
{
ManagementClass partionsClass = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection partions = partionsClass.GetInstances();

string hdd = string.Empty;

foreach (ManagementObject partion in partions)
{
hdd = Convert.ToString(partion["VolumeSerialNumber"]);

if (hdd != string.Empty)
return hdd;
}

return hdd;
}
This static function will simple give us the volume serial numbers.
C# Read Harddisk Serial Number WMI ManagementClass.

25 November 2008

C# WMI Startup Programs, List Startup Applications with ManagementClass

It is possible to list Startup Programs of operating system in C#.

With the use of ManagementClass, a few lines of code will give us this list.
The WMI classes are easy to use such these operations.
  • Add reference System.Management to your project.
  • Add the following code to your code file.
  • using System.Management;
    using System.Management.Instrumentation;
  • Add the code below to list the startup application assigned in os.
  •  ManagementClass mangnmt =
    new ManagementClass("Win32_StartupCommand");

    ManagementObjectCollection mcol = mangnmt.GetInstances();

    foreach (ManagementObject strt in mcol)
    {
    Console.WriteLine("Application Name: "
    + strt["Name"].ToString());

    Console.WriteLine("Application Location: "
    + strt["Location"].ToString());

    Console.WriteLine("Application Command: "
    + strt["Command"].ToString());

    Console.WriteLine("User: " + strt["User"].ToString());
    }

List Autorun programs in C#

C# Read Windows Logon User Name in WMI

Using the namespace System.Management,
we can easily read the Windows Logon User name.

The WMI (Windows Media Instrumentation)
queries are very similar with the SQL queries.

The sample code below written in C# reads the logon user name.
using System.Management;

public static string logonUser()
{
string _user = string.Empty;
ConnectionOptions co = new ConnectionOptions();
System.Management.ManagementScope ms
= new System.Management.ManagementScope("\" + "localhost" + "rootcimv2", co);

System.Management.ObjectQuery oq =
new System.Management.ObjectQuery("SELECT * FROM Win32_ComputerSystem");
ManagementObjectSearcher query =
new ManagementObjectSearcher(ms, oq);

ManagementObjectCollection queryCollection;
queryCollection = query.Get();

foreach (ManagementObject mo in queryCollection)
{
_userl = mo["UserName"].ToString();
}

char[] charSeparators = new char[] { '' };

int deger = _user.Split(charSeparators, StringSplitOptions.None).Length;
_user= _user.Split(charSeparators, StringSplitOptions.None)[deger - 1];

return _user;
}
Will return the Windows Logon User name. Many other WMI queries are available.

C# WMI Howto List Windows OS User List With Login Names

It is not easy to reach windows login users list in C#.

There are several ways to get this list, like WMI (windows media instrumentation).
But here I will use a different way, let's read the registry and list the win user list.
string users_reg_key=
@"SOFTWAREMicrosoftWindowsCurrentVersionExplorerDocFolderPaths";
With using this registry key, we can have all users who login into that os. Let's write a simple function to read this registry key in C#.
 public string[] ListWinUsersList()
{
//The registry key for reading user list.

RegistryKey key =
Registry.LocalMachine.OpenSubKey(users_reg_key, true);

string[] winusers;

if (key != null && key.ValueCount > 0)
{
winusers = key.GetValueNames();
}
return winusers[];
}
And now we get a string array filled with windows users list.