如何使用 .NET Process.Start() 设置应用程序的窗口文本?

Pur*_*ome 2 .net c# console-application

以前,我使用一个.bat文件来运行一些控制台应用程序 .exe。当我这样做时,我可以设置进程窗口标题文本。

例如:-

START "1 - 203.46.105.23:20600 - Sydney 24/7 #1" "C:\MyApplication\Streams\PBUcon\pbucon.exe" ini="C:\MyApplication\Streams\Active\1-203.46.105.23.20600.ini"

所以这将执行文件 pbucon.exe 并将一些参数传递给 exe。控制台窗口标题是1 - 203.46.105.23:20600 - Sydney 24/7 #1

在此处输入图片说明

我不确定如何使用 Process 命令以编程方式执行此操作?

这就是我在做什么...

var processStartInfo = new ProcessStartInfo
    {
        Arguments = string.Format("ini={0}", GameServerFile(gameServer, false)),
        FileName = newPbUconFile,
        WorkingDirectory = ActiveFolder
    };

Process.Start(processStartInfo);
Run Code Online (Sandbox Code Playgroud)

是否可以?

值得一提的是,我还运行了一个控制台应用程序,它启动了那些 pbucon.exe(在需要时)......并做了很多其他的事情。

Séb*_*ier 6

在您的代码中的某处:

[DllImport("user32.dll")]
static extern IntPtr FindWindow(string windowClass, string windowName);

[DllImport("user32.dll")]
static extern bool SetWindowText(IntPtr hWnd, string text);

public void startProcess(string path, string title) {
   Process.Start(path);
   Thread.Sleep(1000); //Wait, the new programm must be full loaded
   IntPtr handle = FindWindow("ConsoleWindowClass", path); //get the Handle of the 
                                                           //console window
   SetWindowText(handle, title); //sets the caption
}
Run Code Online (Sandbox Code Playgroud)

Pure Krome 更新

而不是寻找句柄,该过程已经有该信息......所以我这样做了(因为我开始的程序是一个控制台应用程序,如果这与此有关......)

..... snipped .....
var process = Process.Start(path);
Thread.Sleep(1000); // Wait for the new program to start.
SetWindowText(process.MainWindowHandle, title);
Run Code Online (Sandbox Code Playgroud)

哈。

  • 您可能应该使用“Process.WaitForInputIdle()”而不是睡眠。https://msdn.microsoft.com/en-us/library/kcdbkyt4.aspx (2认同)