Onboard Speakersound (Beep) abspielen

// Aufrufbeispiel
// SpeakerBeep.PlayBeep(1000, 1000);

using System.Runtime.InteropServices;

/// <summary->
/// freeware helper class for creating a onboard system speaker beep
/// (W) 2011 by admin of codezentrale.6x.to
/// </summary->
public static class SpeakerBeep
{
    /// <summary->
    /// http://pinvoke.net/default.aspx/kernel32.Beep
    /// </summary->
    [DllImport(&quot;kernel32.dll&quot;, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool Beep(uint dwFreq, uint dwDuration);
    /// <summary->
    /// Plays a beepsound on onboard speaker
    /// </summary->
    /// <param name=&quot;freq&quot;->desired frequency [Hz]</param->
    /// <param name=&quot;duration&quot;->desired duration [ms]</param->
    /// <returns->true, if successfull, otherwise false</returns->
    public static bool PlayBeep(uint freq, uint duration)
    {
        return Beep(freq, duration);
    }
}

Enum DescriptionAttribute auslesen

using System;
using System.ComponentModel;
using System.Reflection;

/// <summary->
/// freeware helper class for getting enum descriptions
/// (W) 2011 by admin of codezentrale.6x.to
/// </summary->
public static class EnumDescriptor
{
    /// <summary->
    /// get enum attribute description
    /// </summary->
    /// <param name=&quot;val&quot;->current enum</param->
    /// <returns->returns enum attribute description</returns->
    public static string GetDescription(Enum val)
    {
        FieldInfo fi = val.GetType().GetField(val.ToString());
        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        return (attributes.Length -> 0) ? attributes[0].Description : val.ToString();
    }
}

Benutzername und Benutzerolle ausgeben

using System.Security.Principal;

/// <summary->
/// freeware helper class for getting logon user info
/// (W) 2011 by admin of codezentrale.6x.to
/// </summary->
public static class UserInfo
{
    /// <summary->
    /// get logon user name
    /// </summary->
    public static string LogonUserName
    {
        get
        {
            return WindowsIdentity.GetCurrent().Name;
        }
    }
    /// <summary->
    /// get role of current user
    /// </summary->
    public static string LogonUserRole
    {
        get
        {
            string role = string.Empty;

            WindowsPrincipal principle = new WindowsPrincipal(WindowsIdentity.GetCurrent());

            if (principle.IsInRole(WindowsBuiltInRole.AccountOperator)) role = &quot;account operator&quot;;
            else
                if (principle.IsInRole(WindowsBuiltInRole.Administrator)) role = &quot;administrator&quot;;
                else
                    if (principle.IsInRole(WindowsBuiltInRole.BackupOperator)) role = &quot;backup opearator&quot;;
                    else
                        if (principle.IsInRole(WindowsBuiltInRole.Guest)) role = &quot;guest&quot;;
                        else
                            if (principle.IsInRole(WindowsBuiltInRole.PowerUser)) role = &quot;power user&quot;;
                            else
                                if (principle.IsInRole(WindowsBuiltInRole.User)) role = &quot;user&quot;;
                                else
                                    if (principle.IsInRole(WindowsBuiltInRole.PrintOperator)) role = &quot;print operator&quot;;
                                    else
                                        if (principle.IsInRole(WindowsBuiltInRole.Replicator)) role = &quot;replicator&quot;;
                                        else
                                            if (principle.IsInRole(WindowsBuiltInRole.SystemOperator)) role = &quot;system operator&quot;;

            return role;
        }
    }
}

[C#] Assemblyinfo auslesen

using System.IO;
using System.Reflection;

/// <summary>
/// freeware helper class for getting .NET assembly info
/// (W) 2011 by admin of codezentrale.6x.to
/// </summary>
public static class AssemblyInfo
{
    /// <summary>
    /// get assembly title
    /// </summary>
    public static string AssemblyTitle
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);

            if (attributes.Length > 0)
            {
                AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];

                if (titleAttribute.Title != string.Empty) return titleAttribute.Title;
            }

            return Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
        }
    }
    /// <summary>
    /// get assembly version
    /// </summary>
    public static string AssemblyVersion
    {
        get
        {
            return Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }
    }
    /// <summary>
    /// get assembly description
    /// </summary>
    public static string AssemblyDescription
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);

            if (attributes.Length == 0) return string.Empty;

            return ((AssemblyDescriptionAttribute)attributes[0]).Description;
        }
    }
    /// <summary>
    /// get assembly product
    /// </summary>
    public static string AssemblyProduct
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);

            if (attributes.Length == 0) return string.Empty;

            return ((AssemblyProductAttribute)attributes[0]).Product;
        }
    }
    /// <summary>
    /// get assembly copyright
    /// </summary>
    public static string AssemblyCopyright
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);

            if (attributes.Length == 0) return string.Empty;

            return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
        }
    }
    /// <summary>
    /// get assembly company
    /// </summary>
    public static string AssemblyCompany
    {
        get
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);

            if (attributes.Length == 0) return string.Empty;

            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }
    }
}

Speicherauslastung (MEMORYSTATUSEX) auslesen

// Aufruf:
//
// MemInfo.MEMORYSTATUSEX ms = MemInfo.CurrentMemoryStatus;
//
// uint uiMemoryLoad = ms.dwMemoryLoad;
// ulong ulTotalPhys = ms.ulTotalPhysical;
// ulong ulAvailPhys = ms.ulAvailPhysical;
// ulong ulTotalPageFile = ms.ulTotalPageFile;
// ulong ulAvailPageFile = ms.ulAvailPageFile;
// ulong ulTotalVirtual = ms.ulTotalVirtual;
// ulong ulAvailVirtual = ms.ulAvailVirtual;
// ulong ulAvailExtendedVirtual = ms.ulAvailExtendedVirtual;

using System.Runtime.InteropServices;

/// <summary>
/// freeware helper class for getting memory info
/// (W) 2011 by admin of codezentrale.de
/// http://pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html
/// http://msdn.microsoft.com/en-us/library/aa366589%28v=vs.85%29.aspx
/// </summary>
public static class MemInfo
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct MEMORYSTATUSEX
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public ulong ulTotalPhysical;
        public ulong ulAvailPhysical;
        public ulong ulTotalPageFile;
        public ulong ulAvailPageFile;
        public ulong ulTotalVirtual;
        public ulong ulAvailVirtual;
        public ulong ulAvailExtendedVirtual;
    }

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);

    /// <summary>
    /// get current memory status struct
    /// </summary>
    public static MEMORYSTATUSEX CurrentMemoryStatus
    {
        get
        {
            MEMORYSTATUSEX ms = new MEMORYSTATUSEX();
            ms.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));

            GlobalMemoryStatusEx(ref ms);

            return ms;
        }
    }
}

TrailingPathDelimiter für Pfadnamen hinzufügen oder entfernen

using System.IO;

/// <summary>
/// freeware helper class for managing DirectorySeparatorChar
/// (W) 2011 by admin of codezentrale.de
/// </summary>
public static class FileSystem
{
    /// <summary>
    /// include ending DirectorySeparatorChar
    /// </summary>
    /// <param name="path">current path</param>
    /// <returns>path with ending DirectorySeparatorChar</returns>
    public static string IncludeTrailingPathDelimiter(string path)
    {
        return path.EndsWith(Path.DirectorySeparatorChar.ToString()) ? path : path + Path.DirectorySeparatorChar;
    }
    /// <summary>
    /// exclude ending DirectorySeparatorChar
    /// </summary>
    /// <param name="path">current path</param>
    /// <returns>path without ending DirectorySeparatorChar</returns>
    public static string ExcludeTrailingPathDelimiter(string path)
    {
        return path.EndsWith(Path.DirectorySeparatorChar.ToString()) ? path.Remove(path.Length, 1) : path;
    }
}

Informationen über das akt. verwendete .NET-Framework abfragen

using System;
using System.IO;
using Microsoft.Win32;

/// <summary>
/// freeware helper class for getting .NET framework info
/// (W) 2011 by admin of codezentrale.de
/// </summary>
public static class NETFrameworkInfo
{
    /// <summary>
    /// prints the root install path of the .NET-Frameworks on your system
    /// </summary>
    /// <returns>.NET root install path</returns>
    public static string NETRootPath
    {
        get
        {
            string rootpath = string.Empty;

            try
            {
                RegistryKey rKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework");

                if (rKey != null)
                {
                    rootpath = rKey.GetValue("InstallRoot").ToString();
                }
            }
            catch (Exception e)
            {
                rootpath = e.Message;
            }

            return rootpath;
        }
    }
    /// <summary>
    /// prints currently used CLR version string
    /// hint: .NET 3.0 & 3.5 are using CLR v2.0!
    /// </summary>
    /// <returns>currently used CLR version</returns>
    public static string CurrentCLRVersion
    {
        get
        {
            return Environment.Version.ToString();
        }
    }
    /// <summary>
    /// prints the currently used .NET version home directory
    /// hint: .NET 3.0 & 3.5 are using CLR v2.0, so the homedir for the CLR-DLLs is v2.X !
    /// </summary>
    /// <returns>the currently used .NET version home directory</returns>
    public static string CurrentCLRVersionHome
    {
        get
        {
            string sRetVal = string.Empty;

            string sRootPath = NETRootPath;

            if (!string.IsNullOrEmpty(sRootPath))
            {
                sRetVal = Path.Combine(sRootPath, "v" + Environment.Version.Major.ToString() + "." + Environment.Version.Minor.ToString() + "." + Environment.Version.Build.ToString());

                if (!Directory.Exists(sRetVal))
                {
                    sRetVal = string.Empty;
                }
            }

            return sRetVal;
        }
    }
}

Plattformtyp (32Bit, 64Bit) herausfinden

Folgende Klasse bestimmt die Plattform auf welcher der akt. Prozess ausgeführt wird.

using System;

/// <summary>
/// freeware helper class for getting platform info
/// (W) 2011 by admin of codezentrale.de
/// </summary>
public static class PlatFormDetect
{
    private const string sX86 = "x86";
    private const string sAMD64 = "AMD64";

    public enum EPlatformType
    {
        Unknown,
        Native32Bit, // 32Bit process on 32Bit OS
        Native64Bit, // 64Bit process on 64Bit OS
        WOW64        // 32Bit process on 64Bit OS
    }

    /// <summary>
    /// returns the current platform type
    /// </summary>
    public static EPlatformType PlatformType
    {
        get
        {
            EPlatformType ePT = EPlatformType.Unknown;

            // get native process bitness
            // x86   - 32bit native AND WOW64
            // AMD64 - 64bit native
            string sArchitecture = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine);
            // get original CPU architecture
            // undefined (null) == 32Bit
            //            AMD64 == 64Bit
            string sArchitectureW6432 = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432", EnvironmentVariableTarget.Machine);

            // 32Bit process on 32Bit OS
            if ((sArchitecture == sX86) && string.IsNullOrEmpty(sArchitectureW6432)) ePT = EPlatformType.Native32Bit;
            // 64Bit process on 64Bit OS
            if ((sArchitecture == sAMD64) && string.IsNullOrEmpty(sArchitectureW6432)) ePT = EPlatformType.Native64Bit;
            // 32Bit process on 64Bit OS
            if ((sArchitecture == sX86) && (sArchitectureW6432 == sAMD64)) ePT = EPlatformType.WOW64;

            return ePT;
        }
    }
}

Ab .NET-Framework 4.0 stehen die Funktionen Environment.Is64BitProcess und Environment.Is64BitOperatingSystem zur Verfügung.

using System;

// helper class for getting platform info
// (W) 2010 by admin of codezentrale.de
public static class PlatFormDetect4
{
    private const string s32Bit = "32Bit";
    private const string s64Bit = "64Bit";

    /// <summary>
    /// prints the OS bitness
    /// </summary>
    /// <returns>OS bitness</returns>
    public static string OSBitness
    {
        get
        {
            return Environment.Is64BitOperatingSystem ? s64Bit : s32Bit;
        }
    }
    /// <summary>
    /// prints the process bitness
    /// </summary>
    /// <returns>process bitness</returns>
    public static string ProcessBitness
    {
        get
        {
            return Environment.Is64BitProcess ? s64Bit : s32Bit;
        }
    }
}

Windows Betriebssystemversion ermitteln

using System;

/// <summary>
/// freeware helper class for getting OS info
/// (W) 2011 by admin of codezentrale.de
/// </summary>
public static class OSVersion
{
    /// <summary>
    /// version string for current system
    /// </summary>
    public static string VersionString
    {
        get
        {
            string sRetVal = string.Empty;

            Version osver = Environment.OSVersion.Version;

            switch (Environment.OSVersion.Platform)
            {
                // Windows 95 wird von .NET nicht unterstützt
                // Windows 98, Windows 98SE, Windows Me
                case PlatformID.Win32Windows:
                    if (osver.Major == 4)
                    {
                        switch (osver.Minor)
                        {
                            case 10: sRetVal = "Windows 98"; break;
                            case 90: sRetVal = "Windows Me"; break;
                        }
                    }
                    break;
                case PlatformID.Win32NT:
                    // Windows NT 3.51 wird von .NET nicht unterstützt
                    // Windows NT 4.0
                    if (osver.Major == 4) sRetVal = "Windows NT 4.0";

                    // Windows 2000, XP, Server 2003
                    if (osver.Major == 5)
                    {
                        switch (osver.Minor)
                        {
                            case 0: sRetVal = "Windows 2000"; break;
                            case 1: sRetVal = "Windows XP"; break;
                            case 2: sRetVal = "Windows Server 2003"; break;
                        }
                    }
                    // Windows Vista, Windows Server 2008, Windows 7
                    if (osver.Major == 6)
                    {
                        switch (osver.Minor)
                        {
                            case 0: sRetVal = "Windows Vista / Windows Server 2008"; break;
                            case 1: sRetVal = "Windows 7 / Windows Server 2008 R2"; break;
                            case 2: sRetVal = "Windows 8 / Windows Server 2012"; break;
                        }
                    }
                    break;
            }

            if (sRetVal == string.Empty) sRetVal = "unbekannte oder von .NET nicht unterstützte Windows-Version";
            else sRetVal += " [" + Environment.OSVersion.VersionString + ", rev " + osver.Revision.ToString() + "]";

            return sRetVal;
        }
    }
}