प्रोसेस.मेनविंडोटाइटल विज़ुअल स्टूडियो का उपयोग करके खाली स्ट्रिंग लौटाता है


मेरे पास विंडोज़ 11 है। मेरे पास VS2019(C#) में एक कंसोल एप्लिकेशन है।
मैं system.Diagnostics.Process के माध्यम से कैलकुलेटर exe लॉन्च करने का प्रयास कर रहा हूं।

सांकेतिक टुकड़ा:

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

उपरोक्त कोड चलाने के बाद, कैलकुलेटर exe लॉन्च हो रहा है लेकिन myProcess.MainWindowTitle एक खाली स्ट्रिंग लौटाता है। मैं इस बात को समझ नहीं पा रहा हूं. कृपया मदद करे।

मैंने क्या प्रयास किया है:

MainWidowsTitle का मान प्राप्त करने के लिए मैंने उपरोक्त कोड चलाने का प्रयास किया है। लेकिन भले ही कैलकुलेटर लॉन्च हो रहा हो, ‘मेनविंडोटाइटल’ का मान खाली स्ट्रिंग है।

समाधान 1

C:\Windows\System32\calc.exe केवल एक स्टब है जो एक अन्य प्रक्रिया शुरू करता है। यदि आप अपनी प्रक्रिया वस्तु को देखते हैं तो वह बाहर निकल चुकी होगी।

मेरे विंडोज़ 10 सिस्टम पर calc.exe वास्तव में कैलकुलेटर “ऐप” शुरू करता है जो कि है
C:\प्रोग्राम फ़ाइलें\WindowsApps\Microsoft.Windowsकैलकुलेटर_10.2103.8.0_x64__8wekyb3d8bbwe\कैलकुलेटर.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をコピーしました