Process.mainwindowtitle mengembalikan string kosong menggunakan visual studio

pemrograman


Saya memiliki jendela 11. Saya memiliki aplikasi konsol di VS2019(C#).
Saya mencoba meluncurkan kalkulator exe melalui system.Diagnostics.Process.

cuplikan kode:

C#
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\calc.exe");

            //startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.WindowStyle = ProcessWindowStyle.Normal;

            var myProcess = Process.Start(startInfo);

            myProcess.WaitForInputIdle();

            Console.Write("Main window Title : " + myProcess.MainWindowTitle.ToString());

Setelah menjalankan kode di atas, exe kalkulator diluncurkan tetapi myProcess.MainWindowTitle mengembalikan string kosong. Saya tidak dapat memahami fakta ini. Tolong bantu.

Apa yang saya coba:

Saya telah mencoba menjalankan kode di atas untuk mengambil nilai MainWidowsTitle. Namun meskipun kalkulator diluncurkan, nilai ‘MainWindowTitle’ adalah string kosong.

Solusi 1

C:\Windows\System32\calc.exe hanyalah sebuah rintisan yang memulai proses lain. Jika Anda melihat objek proses Anda, objek itu akan keluar.

Di sistem windows 10 saya, calc.exe sebenarnya memulai “aplikasi” kalkulator
C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.2103.8.0_x64__8wekyb3d8bbwe\Calculator.exe

Itu tidak dapat dimulai secara langsung.

Solusi 2

Masalah Anda mungkin terkait dengan waktu antara menjalankan kode dan memanggil Judul jendela. Anda perlu memeriksa pegangan jendela utama sebelum mencoba mengambil judul setelah proses memasuki keadaan siaga.

Saya telah menambahkan metode untuk Anda yang disebut ‘GetMainWindowTitle’ untuk memeriksa pegangan jendela utama –

C#
using System;
using System.Diagnostics;

class RunandReadMyProgram
{
    static void Main()
    {
        //Create a new process start info...
        ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\calc.exe");

        //Configure your startInfo properties...
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Normal;

        //Start your processing...
        var myProcess = Process.Start(startInfo);

        //Wait for your process to enter the idle state before reading the title...
        myProcess.WaitForInputIdle();

        //Retrieve the main window title once your process is idle...
        string mainWindowTitle = GetMainWindowTitle(myProcess);

        //Display the main window title where you need it...
        Console.WriteLine("Calculator Window Title: " + mainWindowTitle);
    }

    //The method to get the main window title of your process...
    static string GetMainWindowTitle(Process process)
    {
        string title = string.Empty;

        //Check if your process has a main window handle...
        if (process.MainWindowHandle != IntPtr.Zero)
        {
            //Read the title of the main window...
            title = process.MainWindowTitle;
        }

        return title;
    }
}

コメント

タイトルとURLをコピーしました