简体   繁体   中英

How to get saved location of All opened Notepad Files in C#

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM