يقوم Process.mainwindowtitle بإرجاع سلسلة فارغة باستخدام الاستوديو المرئي


عندي ويندوز 11 . لدي تطبيق وحدة تحكم في VS2019 (C #).
أحاول إطلاق آلة حاسبة إكس من خلال system.Diagnostics.Process.

مقتطف الكود:

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

بعد تشغيل التعليمات البرمجية أعلاه، يتم تشغيل برنامج الآلة الحاسبة ولكن myProcess.MainWindowTitle يقوم بإرجاع سلسلة فارغة. أنا غير قادر على فهم هذه الحقيقة. الرجاء المساعدة.

ما حاولت:

لقد حاولت تشغيل التعليمات البرمجية أعلاه لجلب قيمة MainWidowsTitle. ولكن على الرغم من بدء تشغيل الآلة الحاسبة، فإن قيمة ‘MainWindowTitle’ هي سلسلة فارغة.

الحل 1

C:\Windows\System32\calc.exe هو مجرد كعب روتين يبدأ عملية أخرى. إذا نظرت إلى كائن العملية الخاص بك، فسيتم الخروج منه.

على نظام Windows 10 الخاص بي، يقوم calc.exe بالفعل بتشغيل “تطبيق” الآلة الحاسبة وهو
C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.2103.8.0_x64__8wekyb3d8bbwe\Calculator.exe

لا يمكن أن تبدأ مباشرة.

الحل 2

قد تكون مشكلتك مرتبطة بالتوقيت بين تشغيل التعليمات البرمجية الخاصة بك واستدعاء عنوان النافذة. يتعين عليك التحقق من مقبض النافذة الرئيسية قبل محاولة استرداد العنوان بعد دخول العملية في حالة الخمول.

لقد أضفت طريقة لك تسمى “GetMainWindowTitle” للتحقق من مقبض النافذة الرئيسية –

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