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

标签 c# .net 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

enter image description here

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

这是我正在做的...

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

Process.Start(processStartInfo);

这可能吗?

为了它的值(value),我还运行了一个控制台应用程序,该应用程序启动了那些 pbucon.exe(需要时)...并执行许多其他操作。

最佳答案

代码中的某处:

[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
}

由 Pure Krome 更新

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

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

HTH.

关于c# - 如何使用 .NET Process.Start() 设置应用程序的窗口文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15988917/

相关文章:

c# - 如何将参数传递给 C# 中的公共(public)类?

c# - 条件绑定(bind)重定向

c# - X509 证书预期用途和 SSL

c# - 将轻量级 Web 服务器嵌入到 .net 应用程序(node.js)中?

c# - 使用Base64编码的公钥验证RSA签名

c# - 如何在解决方案中集成测试控制台应用程序?

c# - "The process cannot access the file X because it is used by another process"-C#

c# - 如何从 Controller 将数据传递到 View 中的jquery

c# - 为什么将 DoEvents 放入循环中会导致 StackOverflow 异常?

c# - ASP.NET MVC : Ignore field(s) when editing