C# 进程名称,但不像“chrome(.exe)”但就像“Chrome”一样
也许是一个奇怪的问题,但我真的不知道如何问它,希望我在标题中清楚地表达了我的观点。
到目前为止我所拥有的代码:
/// <summary>
/// Get the process that is currently running on the foreground
/// </summary>
/// <returns>Proces object containing all information about the process that is running on the foreground</returns>
public static Process GetCurrentRunningProcess()
{
Process[] processes = Process.GetProcesses();
IntPtr activeWindow = GetForegroundWindow();
foreach (Process process in processes)
{
if (process.MainWindowHandle == activeWindow)
return process;
}
return null;
}
/// <summary>
/// Get the Foreground Window using the user32.dll
/// </summary>
/// <returns>The handle of the window</returns>
[DllImport("user32", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();
这就是我现在正在做的事情,它为我提供了包含 ProcessName 的 Process。就我而言,我在实际想要“Chrome”的地方得到“chrome”,或者当我有记事本(.exe)时我想要记事本。有办法实现这一点吗?或者我是否必须列出程序名称并将其与 ProcessName 进行比较?
Maybe a weird question, but i did not really know how to ask it, hope i made my point clear in the title.
Code i have so far:
/// <summary>
/// Get the process that is currently running on the foreground
/// </summary>
/// <returns>Proces object containing all information about the process that is running on the foreground</returns>
public static Process GetCurrentRunningProcess()
{
Process[] processes = Process.GetProcesses();
IntPtr activeWindow = GetForegroundWindow();
foreach (Process process in processes)
{
if (process.MainWindowHandle == activeWindow)
return process;
}
return null;
}
/// <summary>
/// Get the Foreground Window using the user32.dll
/// </summary>
/// <returns>The handle of the window</returns>
[DllImport("user32", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();
This is what I'm doing right now, and it get's me the Process which contains a ProcessName. In my case I get 'chrome' where i actually want 'Chrome' or when I have notepad(.exe) i want Notepad. Is there a way to achieve this? Or do I have to make a list with program names and compare it with the ProcessName?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在大多数情况下,查找表比尝试重新格式化可执行文件名称更好,因为许多程序没有以最明显的方式命名(Microsoft Word 是 WINWORD.exe)。
我还没有测试
Process.MainWindowTitle
,但您可能会发现它包含打开文档的名称和其他您不想要的垃圾。最好在尝试任何奇特的东西之前对其进行测试。For the most part, a lookup table is better than trying to reformat the executable name as many programs are not named in the mosst obvious way (Microsoft Word is WINWORD.exe).
I haven't tested
Process.MainWindowTitle
, but you may find that it includes the names of open documents and other rubbish you don't want. Best to test it before trying anything fancy.您可以使用的是
Process.MainWindowTitle
What you can just use is
Process.MainWindowTitle
替换“(.exe)”并将第一个字母转换为大写怎么样?还是我错过了重点?
What about replacing the "(.exe)" and casting the first letter to upper? Or do I miss the point?