Wednesday, December 24, 2014

Get System Information using C# Operating System, OS Version, 32/64 bit, System Directory, Processor Count, User Name, Domain, Drive information (drive label, drive type, Format, Size, Free space) PART - 1

Description:
Following code can be used to get system information like Operation System, OS Version, 32/64 bit, System Directory, Processor Count, User Name, Domain, Drive information (drive label, drive type, Format, Size, Free space) etc..


private string SystemInformation()
{
    StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
    try
    {
        StringBuilder1.AppendFormat("Operation System:  {0}\n"Environment.OSVersion);
        if (Environment.Is64BitOperatingSystem)
            StringBuilder1.AppendFormat("\t\t  64 Bit Operating System\n");
        else
            StringBuilder1.AppendFormat("\t\t  32 Bit Operating System\n");
        StringBuilder1.AppendFormat("SystemDirectory:  {0}\n"Environment.SystemDirectory);
        StringBuilder1.AppendFormat("ProcessorCount:  {0}\n"Environment.ProcessorCount);
        StringBuilder1.AppendFormat("UserDomainName:  {0}\n"Environment.UserDomainName);
        StringBuilder1.AppendFormat("UserName: {0}\n"Environment.UserName);
        //Drives
        StringBuilder1.AppendFormat("LogicalDrives:\n");
        foreach (System.IO.DriveInfo DriveInfo1 in System.IO.DriveInfo.GetDrives())
        {
            try
            {
                StringBuilder1.AppendFormat("\t Drive: {0}\n\t\t VolumeLabel: " +
                    "{1}\n\t\t DriveType: {2}\n\t\t DriveFormat: {3}\n\t\t " +
                    "TotalSize: {4}\n\t\t AvailableFreeSpace: {5}\n",
                    DriveInfo1.Name, DriveInfo1.VolumeLabel, DriveInfo1.DriveType,
                    DriveInfo1.DriveFormat, DriveInfo1.TotalSize, DriveInfo1.AvailableFreeSpace);
            }
            catch
            {
            }
        }
        StringBuilder1.AppendFormat("SystemPageSize:  {0}\n"Environment.SystemPageSize);
        StringBuilder1.AppendFormat("Version:  {0}"Environment.Version);
    }
    catch
    {
    }
    return StringBuilder1.ToString();
}
Following function can be used to get free space in a drive:

static void GetTotalFreeSpace()
{
    foreach (DriveInfo drive in DriveInfo.GetDrives())
    {
        if (drive.IsReady)
            strOutput += drive.Name + " : " + drive.TotalFreeSpace + ",";
    }
}


No comments:

Post a Comment