C# 进程名称,但不像“chrome(.exe)”但就像“Chrome”一样

发布于 2024-11-03 13:30:56 字数 1121 浏览 1 评论 0原文

也许是一个奇怪的问题,但我真的不知道如何问它,希望我在标题中清楚地表达了我的观点。

到目前为止我所拥有的代码:

    /// <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕梦 2024-11-10 13:30:56

在大多数情况下,查找表比尝试重新格式化可执行文件名称更好,因为许多程序没有以最明显的方式命名(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.

旧时模样 2024-11-10 13:30:56

您可以使用的是 Process.MainWindowTitle

What you can just use is Process.MainWindowTitle

如日中天 2024-11-10 13:30:56

替换“(.exe)”并将第一个字母转换为大写怎么样?还是我错过了重点?

public string ToUpperFirstLetter(string source)
{
    if (String.IsNullOrEmpty(source))
        return String.Empty;
    // convert to char array of the string
    var letters = source.Replace(".exe", String.Empty).ToCharArray();
    // upper case the first char
    letters[0] = Char.ToUpper(letters[0], CultureInfo.CurrentCulture);
    // return the array made of the new char array
    return new string(letters);
}

What about replacing the "(.exe)" and casting the first letter to upper? Or do I miss the point?

public string ToUpperFirstLetter(string source)
{
    if (String.IsNullOrEmpty(source))
        return String.Empty;
    // convert to char array of the string
    var letters = source.Replace(".exe", String.Empty).ToCharArray();
    // upper case the first char
    letters[0] = Char.ToUpper(letters[0], CultureInfo.CurrentCulture);
    // return the array made of the new char array
    return new string(letters);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文