如何使用Windows窗体应用程序获取活动进程文件名?

时间:2015-01-29 15:03:38

标签: c# winforms winforms-interop

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]”正确地获取当前打开的文件名.. 如果我成功运行我的application.executed ..现在 我逐个打开3个记事本文件,如abc.txt,aaa.txt,dde.txt,这将打开文件将正常显示...

如果我逐个打开word文档3个文件或excel文件..我只获得第一个打开的文件名只保存... 如何获得正确的打开文档结果为什么我在打开word或excel或pdf文件打开环境时遇到此问题...

1 个答案:

答案 0 :(得分:0)

使用以下代码获取单词文件实例

 private void timer1_Tick(object sender, EventArgs e)
    {

        y = GetActiveWindowTitle();

        try
        {
            Microsoft.Office.Interop.Word.Application WordObj;
            WordObj = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
            var vvv = WordObj.StartupPath;
                x = "";
                for (int i = 0; i < WordObj.Windows.Count; i++)
                {
                    object idx = i + 1;
                    Microsoft.Office.Interop.Word.Window WinObj = WordObj.Windows.get_Item(ref idx);

                    // doc_list.Add(WinObj.Document.FullName);


                    x = x + "," + WinObj.Document.FullName;

                    //x = WinObj.Document.FullName;
                }                                                         

        }
        catch (Exception ex)
        {

            // No documents opened
        }

        string[] ax=x.Split(',');
      //  string[] ax1 = x1.Split(',');




        ForAllWordFiles.Text = x;
        ForWordTitle.Text = y;
        if (y != null)
        {
            ActiveWord.Text = " ";
            if (y.Contains("- Microsoft Word"))
            {
                ForWordTitle.Text = y.Substring(0, y.Length - 17);


                foreach (var item in ax)
                {
                    if (item.Contains(ForWordTitle.Text))
                    {
                        ActiveWord.Text = item;
                        break;
                    }
                    ActiveWord.Text = " ";
                }

            }
        }


    }

此代码适用于2010办公室