I am trying to map the window titles of running applications to their corresponding executable names in C#. Currently, I am using the GetWindowText() method to retrieve the window title and manually mapping the title to the executable name.
Here’s my current implementation:
public string GetWindowTitle(IntPtr window)
{
StringBuilder title = new StringBuilder(10000);
if (GetWindowText(window, title, 10000) > 0)
{
return title.ToString();
}
return "";
}
public string GetGeneralName(string windowTitle)
{
if (windowTitle.Contains("Microsoft Visual Studio"))
{
return "devenv";
}
if (windowTitle.Contains("Brave"))
{
return "brave";
}
if (windowTitle.Contains("Google Chrome"))
{
return "chrome";
}
if (windowTitle.Contains("Visual Studio Code"))
{
return "Code";
}
return "";
}
The problem with this approach is that it doesn’t work for window titles that are not listed in the GetGeneralName method.
For example, if the window title is “Process – Microsoft Visual Studio”, I want to map it to “devenv” dynamically without hardcoding the mapping.
CodeCrafter20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.