jimmy jimmy - 5 days ago 6
C# Question

Getting output from DxDiag in C#

So I am trying to automate the output from DxDiag in C# and I have run into a problem. The program runs but doesn't produce any output file. It might be that I am not passing parameters correctly or that I am misunderstanding something.

When running DxDiag in the normal command line I do this and it works as expected:

dxdiag -64bit -x C:\dxFromCode.xml


And this is how I am trying to do it in code:

//Create process
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();

pProcess.StartInfo.FileName = "dxdiag";

pProcess.StartInfo.Arguments = @"64bit x C:\dxFromCode.xml";

pProcess.StartInfo.UseShellExecute = false;

//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = false;

//Start the process
pProcess.Start();

//Wait for process to finish
pProcess.WaitForExit();


EDIT:

Ok, so I changed my code to build for x64. This makes dxdiag automatically start the as the 64-bit version. Then I could just take away the 64bit switch and suddenly everything works as I would expect.

Answer

DxDiag is quite cranky on a 64-bit OS. The 32-bit version and the 64-bit version accept different command line switches and it doesn't give a peep when you use the wrong one. The /x option simply does not work when you try to use the /64bit option on the 32-bit version. And the 64-bit version does not accept /64bit. You'll have to start the 64-bit version explicitly when you run on a 64-bit OS and your program runs in 32-bit mode.

This worked well on my Win81 x64 machine:

    private string RunDxDiag() {
        var psi = new ProcessStartInfo();
        if (IntPtr.Size == 4 && Environment.Is64BitOperatingSystem) {
            // Need to run the 64-bit version
            psi.FileName = System.IO.Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.Windows),
                "sysnative\\dxdiag.exe");
        }
        else {
            // Okay with the native version
            psi.FileName = System.IO.Path.Combine(
                Environment.SystemDirectory, 
                "dxdiag.exe");
        }
        string path = System.IO.Path.GetTempFileName();
        try {
            psi.Arguments = "/x " + path;
            using (var prc = Process.Start(psi)) {
                prc.WaitForExit();
                if (prc.ExitCode != 0) {
                    throw new Exception("DXDIAG failed with exit code " + prc.ExitCode.ToString());
                }
            }
            return System.IO.File.ReadAllText(path);
        }
        finally {
            System.IO.File.Delete(path);
        }
    }
Comments