如何使用Windows窗体应用程序获取活动进程文件名? [英] How to get Active process file name using windows forms application?

查看:97
本文介绍了如何使用Windows窗体应用程序获取活动进程文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static string GetActiveProcessFileName()
       {
           try
           {
               IntPtr hwnd = GetForegroundWindow();
               uint pid;
               GetWindowThreadProcessId(hwnd, out pid);
               Process p = Process.GetProcessById((int)pid);
               // return p.MainModule.FileName;
               CommandLine = GetMainModuleFilepath((int)pid);
               if (CommandLine != null)
               {
                   var array = CommandLine.Split('"');

                   if (array.Length == 3)
                   {
                       if (array[array.Length - 1].Equals(" "))
                       {
                           return "Application";
                       }
                       if (!array[array.Length - 1].Equals(" "))
                       {
                           return array[array.Length - 1];
                       }
                       return null;
                   }
                   if (array.Length == 5)
                   {
                       return array[array.Length - 2];
                   }
                   return "Explorer";
               }
               return "Explorer";
           }
           catch (Exception ex)
           {
               ErrorLog.ErrorLog.Log(ex);
               return "Explorer";
           }



在这里,我正确地获得了[CommandLine]当前打开的文件名..

例如,如果我打开3个记事本文件,例如abc.txt,aaa.txt,dde.txt
如果我运行我的应用程序...哪个打开的文件将正常显示...

如果我一次打开Word文档3个文件或excel文件...我只会得到第一个打开的文件名...
我怎样才能得到正确的结果...请帮助我..



Here i get [CommandLine] current open file names correnctly..

For example if i open 3 notepad files like abc.txt,aaa.txt,dde.txt
if i run my application...Which will opened file will be display as normal...

If i opened word documents 3 file or excel files at a time...I get only first opened file names only...
How can i get correct result of this...Please help me..

推荐答案

我不确定您的目标,所以也许您需要所有的代码上面写的,但是如果您只是在使用记事本打开所有文件(例如,您可以将其扩展到Word,Excel等),则可以使用下面的代码来返回一系列进程.

I''m not sure your goal so maybe you need all the code you have written above but if you are just after all the open files using notepad (as an example, you can extend this to Word, Excel, etc.) you can use the below code which will return an array of processes.

Process[] lstNotepad = Process.GetProcessesByName("notepad");



然后,如果要查找文件名,则可以通过访问MainWindowTitle属性来访问数组中的每个元素:



and then if you want to find the filename you can access of each element in the array by accessing the MainWindowTitle property:

lstNotepad[0].MainWindowTitle



您必须在类中添加System.Diagnostics引用才能使用Process函数.

会满足您的要求吗?



you will have to add the System.Diagnostics reference at to your class to use the Process function.

Does that do what you needed it to?


这篇关于如何使用Windows窗体应用程序获取活动进程文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆