如何获取记事本文件的保存位置 [英] How to get Notepad file saved location
问题描述
如果记事本文件保存在驱动器中,则如何检索其实际路径.例如,一个记事本进程正在运行,并将其保存在驱动器中的某个位置.如何获取其完整路径?使用下面的代码,我可以获取过程详细信息,但不能获取特定文件的实际路径.
How to retrieve the actual 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 executeable path but i need Drive location whre the actual file reside.
推荐答案
这应该做到:
string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", "notepad.exe");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();
foreach (ManagementObject retObject in retObjectCollection)
{
string CommandLine = retObject["CommandLine"].ToString();
string path = CommandLine.Substring(CommandLine.IndexOf(" ") + 1, CommandLine.Length - CommandLine.IndexOf(" ") - 1);
}
仅当通过双击或通过命令行打开文件时,它才有效.
It will work only if the file is opened by double click or through command line.
不要忘记通过右键单击项目",添加引用",然后选择程序集"选项卡并搜索"System.Management"来添加对System.Management
的引用.
Don't forget to add reference to System.Management
by right Click on Project, Add References then select the Assemblies Tab and Search for System.Management.
这篇关于如何获取记事本文件的保存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!