我当前使用的是:
String Title = args[0].ToLower()+ " - Notepad";
Process process = Process.GetProcesses().Where(p => p.MainWindowTitle
== Title).SingleOrDefault();
我一直在寻找一种同时获得这两个条件的方法,因为参数0中传递的文件可能在另一种语言的记事本中打开,所以我必须处理一个不必要的巨大可能性,而不是编辑代码,以获得在当前使用的系统语言的记事本窗口标题中可能字符串的几种语言的结果:
File_Name.eXtension - Notepad
//in:en-EUFile_Name.eXtension - Bloco de Notas
in:/en-br
我正在尝试类似于的东西
...
[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
...
String Title = args[0].ToLower()+ " - Notepad";
String Exec = System.IO.Path.GetFileNameWithoutExtension(args[1].ToLower());
Process process = Process.GetProcesses().Where(p => p.MainWindowTitle
== Title&& p.ProcessName == Exec).SingleOrDefault();
if (process != null)
{
var wHnd = process.MainWindowHandle;
if (!IsIconic(wHnd))
{
Console.WriteLine("False"); // Maximized or Nommal Window: File - Notepad.exe //
return;
}
else if (IsIconic(wHnd));
{
Console.WriteLine("True"); // Minimized Window: File - Notepad.exe //
return;
}
}
Console.WriteLine("File not open!"); // Not Founded Windows //
这样,只有当打开的文件被记事本打开时,才有可能获得bool,从而绑定已经与args[0]
关联并与args[1]
切换的窗口条件和状态。
我可以添加一个对cmd的调用,并使其类似,然后我会保存结果并根据返回进行操作,但它已经返回了很多,我知道方法中一定有类似的东西,然而,我相信我使用的表达式(我的英语很差(要求不要帮我找到相同处理的方法。。。
wmic process where "Name like '%Notepad.exe%'andCommandLine like '%\File_Name.eXtension%'" get process id.
The case of Notepad in particular, is that it can have a window name that varies according to the language of the windows, and is a pre-defined "standard" editor.
The case is summarized in how to obtain 2 conditions in a process, where I will have a title in one condition and a process name in another condition.
Where
filters all processes to those that match the condition.SingleOrDefault
will search for a single process that matches the condition and if multiple or no process matches it will returnnull
. This is what you should do:
Process process = Process
.GetProcesses()
.SingleOrDefault(p => p.MainWindowTitle == Title && p.ProcessName == Exec);