Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
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";
           }


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..
Posted

1 solution

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.

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


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

C#
lstNotepad[0].MainWindowTitle


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?
 
Share this answer
 
Comments
santhu888 29-Jan-15 10:45am    
firstly If i opened Ex.docx file ..then process file name is Ex.docx...It is in opened state...Now i open another word file Like Ex2.docx file...Now i want to display Ex2.docx is opened.But it shows First opened file Ex.docx name will be displayed every time...If i close Ex.docx file then it shows file name is Ok.
MrGlass3 29-Jan-15 10:59am    
I stand corrected, the code I provided above would work for Notepad because every file that is opened creates a new Notepad process. For Microsoft Office applications there is only a single process which is created and that process manages all opened files. I believe to get at the information you want you will have to use an interrop service...perhaps something like which is described here http://stackoverflow.com/questions/7916711/get-the-current-workbook-object-in-c-sharp although I have not tested this.
santhu888 30-Jan-15 2:33am    
Yes,Exactly you got it my point..But where we can write these steps...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900