Process.MainWindowTitle 屬性

定義

取得處理序的主視窗標題。

public:
 property System::String ^ MainWindowTitle { System::String ^ get(); };
public string MainWindowTitle { get; }
member this.MainWindowTitle : string
Public ReadOnly Property MainWindowTitle As String

屬性值

處理序的主視窗標題。

例外狀況

因為處理序已經結束,所以未定義 MainWindowTitle 屬性。

您正在嘗試存取於遠端電腦上執行之處理序的 MainWindowTitle 屬性。 這個屬性僅供在本機電腦執行的處理序使用。

範例

下列範例會啟動記事本的實例,並擷取進程主視窗的 標題。

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
int main()
{
   try
   {
      
      // Create an instance of process component.
      Process^ myProcess = gcnew Process;
      
      // Create an instance of 'myProcessStartInfo'.
      ProcessStartInfo^ myProcessStartInfo = gcnew ProcessStartInfo;
      myProcessStartInfo->FileName = "notepad";
      myProcess->StartInfo = myProcessStartInfo;
      
      // Start process.
      myProcess->Start();
      
      // Allow the process to finish starting.
      myProcess->WaitForInputIdle();
      Console::Write( "Main window Title : {0}", myProcess->MainWindowTitle );
      myProcess->CloseMainWindow();
      myProcess->Close();
   }
   catch ( Exception^ e ) 
   {
      Console::Write( " Message : {0}", e->Message );
   }

}
using System;
using System.Diagnostics;

class MainWindowTitleClass
{
    public static void Main()
    {
        try
        {
            // Create an instance of process component.
            using (Process myProcess = new Process())
            {
                // Create an instance of 'myProcessStartInfo'.
                ProcessStartInfo myProcessStartInfo = new ProcessStartInfo();
                myProcessStartInfo.FileName = "notepad";
                myProcess.StartInfo = myProcessStartInfo;
                // Start process.
                myProcess.Start();
                // Allow the process to finish starting.
                myProcess.WaitForInputIdle();
                Console.Write("Main window Title : " + myProcess.MainWindowTitle);

                myProcess.CloseMainWindow();
            }
        }
        catch (Exception e)
        {
            Console.Write($" Message : {e.Message}");
        }
    }
}
Imports System.Diagnostics

Class MainWindowTitleClass
    Public Shared Sub Main()
        Try
            ' Create an instance of process component.
            Using myProcess As New Process()
                ' Create an instance of 'myProcessStartInfo'.
                Dim myProcessStartInfo As New ProcessStartInfo()
                myProcessStartInfo.FileName = "notepad"
                myProcess.StartInfo = myProcessStartInfo
                ' Start process.
                myProcess.Start()
                ' Allow the process to finish starting.
                myProcess.WaitForInputIdle()
                Console.Write("Main window Title : " + myProcess.MainWindowTitle)

                myProcess.CloseMainWindow()
            End Using
        Catch e As Exception
            Console.Write($" Message : {e.Message}")
        End Try
    End Sub
End Class

備註

只有在進程具有圖形化介面時,進程才會有與其相關聯的主視窗。 如果相關聯的進程沒有主視窗 (,因此為 MainWindowHandle 零) ,或者如果系統無法判斷有主視窗 (,例如在某些 Unix 平臺上 MainWindowTitle) ,則為空字串 (“”) 。

如果您剛啟動進程並想要使用主視窗標題,請考慮使用 WaitForInputIdle 方法來允許進程完成啟動,以確保已建立主視窗句柄。 否則,系統會擲回例外狀況 (Exception)。

注意

主視窗是目前具有焦點的視窗;請注意,這可能不是進程的主要視窗。 如果物件已變更,您必須使用 Refresh 方法來重新 Process 整理 物件,以取得最新的主視窗句柄。

適用於