0

How to retrieve the full path of the notepad file if it is saved in drive. For example a notepad process is running and it is saved somewhere in the drive. How can I retrieve its full path? Using below code I can get the process detail, but not the actual path of particular files.

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

foreach (Process p in localByName)
{
    string path  =  p.MainModule.FileName.ToString();
}

this returns C:\WINDOWS\system32\NOTEPAD.EXE I also want to get saved location of file.

Nafees Abbasi
  • 367
  • 1
  • 4
  • 12
  • My guess is that you can't do this. What are you trying to do with this app? – Gabriel Duarte Jan 06 '16 at 22:35
  • I want to save the detail so I can reopen it on one click .it can b multiple notepad files – Nafees Abbasi Jan 06 '16 at 23:05
  • Here you can get your ans: [Please see this thread](http://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c) – Surya Jan 07 '16 at 09:26

1 Answers1

1

You can't get this easily. You need the file handles for a particular process. Notepad (at least on my system) doesn't seem to keep the file handle open to a file - it opens it then closes the handle (because process explorer doesn't find the text file when i search by handle). The file name will be in the window title, and you can also get that text through Windows API calls (and possibly the process api) but that won't give you the file path.

You can test this by going into Process Explorer, opening your text file in notepad, and then checking for the file under 'handles'. Mine don't show up.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • when we put our system on sleep mode, how it manage our current activities? – Nafees Abbasi Jan 07 '16 at 09:31
  • it doesn't change - sleep mode is just a low power 'pause' mode (its still consuming power). nothing needs to reboot, it just continues where it left off. Notepad doesn't do anything, this is an OS and CPU level procedure. Hibernate dumps the contents of memory to disk and then resumes it when you leave hibernation. When you come out of sleep mode processing continues. If you want a notepad that resumes after a shutdown, it would be a custom developed solution that writes temp files, such as sublime does. – Adam Tuliper Jan 07 '16 at 20:45
  • Even [handle](https://technet.microsoft.com/en-us/sysinternals/handle.aspx) or [openfiles](https://technet.microsoft.com/en-us/library/bb490961.aspx) doesn't find it.. You'll have to read the tasklist/processlist and try to get it from the window's title (if exposed). – Yves Schelpe Feb 01 '17 at 03:28