![](/img/trans.png)
[英]How to get rid of the pause command in the batch file which is opened from the command prompt using c#
[英]How to get the path of the file which is opened in an application from the running process in c#
我需要通過使用該過程來獲取在應用程序中打開的文件的完整路徑。
例如,我在記事本中從C:\\Users\\Admin\\Documents\\sample.txt
打開了一個txt
文件。 而我有兩個記事本正在桌面中打開。 如果我使用Process.GetProcessesByName("notepad");
我可能會獲得所有正在運行的記事本實例, 甚至可以使用Mainmodule.Filename
獲得正在運行的進程的完整路徑。
但是我的要求是我需要從進程中獲取文件的路徑(即sample.txt
) 。 可能嗎?
因此,我嘗試過:
Process[] localByName = Process.GetProcessesByName("EA");
string path = localByName[0].Modules[0].FileName;
foreach (Process process in localByName)
{
}
這完全取決於打開文本文件的應用程序是否擁有打開文件的句柄。 例如,Notepad.exe將打開文本文件,復制其內容並釋放句柄。 窗口標題欄為您提供的信息不多。
相反,如果通過winword.exe打開文件,則應用程序將保留文本文件的句柄。 在這種情況下,您可以從命令行(或通過Process.Start())使用Mark Russinovich的handle.exe,如下所示:
handle.exe test.txt
這將為您提供有關text.txt完整路徑的信息,您的應用程序可以解析和使用該路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.