Search code examples
c#.netuuid

how to get Device UUID using C#


I want to get Device UUID using C#. The ID I needs can get using wmic csproduct get uuid cmd command. I try to run this command in C# cmd process, but it not gives the only UUID as output. It gives all of the cmd text as output. So, how could i get Device UUID in C#. I use .net framework 4.7.2.


Solution

  • You can apply a regular expression to filter the output from wmic:

    private string GetUuid()
    {
        try
        {
            var proc = Process.Start(new ProcessStartInfo
            {
                FileName = "wmic.exe",
                Arguments = "csproduct get uuid",
                RedirectStandardOutput = true
            });
            if (proc != null)
            {
                string output = proc.StandardOutput.ReadToEnd();
                // Search for UUID string
                var match = System.Text.RegularExpressions.Regex.Match(output, @"[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}");
                if (match.Success) { return match.Value; }
            }
        }
        catch { }  // Your exception handling
        return null;  // Or string.Empty
    }