Process.mainwindowtitle trả về chuỗi trống bằng cách sử dụng studio trực quan

lập trình


Tôi có cửa sổ 11. Tôi có ứng dụng bảng điều khiển trong VS2019(C#).
Tôi đang cố gắng khởi chạy máy tính exe thông qua system.Diagnostics.Process.

đoạn mã:

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());

Sau khi chạy đoạn mã trên, exe máy tính sẽ được khởi chạy nhưng myProcess.MainWindowTitle trả về một chuỗi trống. Tôi không thể hiểu được sự thật này. Hãy giúp tôi.

Những gì tôi đã thử:

Tôi đã thử chạy mã ở trên để lấy giá trị của MainWidowsTitle. Nhưng ngay cả khi máy tính được khởi chạy, giá trị của ‘MainWindowTitle’ vẫn là chuỗi trống.

Giải pháp 1

C:\Windows\System32\calc.exe chỉ là một phần sơ khai khởi động một quá trình khác. Nếu bạn nhìn vào đối tượng quy trình của mình thì nó sẽ thoát.

Trên hệ thống Windows 10 của tôi, calc.exe thực sự khởi động “ứng dụng” máy tính.
C:\Tệp chương trình\WindowsApps\Microsoft.WindowsCalculator_10.2103.8.0_x64__8wekyb3d8bbwe\Calculator.exe

Nó không thể được bắt đầu trực tiếp.

Giải pháp 2

Sự cố của bạn có thể liên quan đến thời gian giữa việc chạy mã và gọi Tiêu đề của cửa sổ. Bạn cần kiểm tra tay cầm cửa sổ chính trước khi thử truy xuất tiêu đề sau khi quá trình đã chuyển sang trạng thái không hoạt động.

Tôi đã thêm cho bạn một phương thức có tên là ‘GetMainWindowTitle’ để kiểm tra tay cầm cửa sổ chính –

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をコピーしました