如何获取记事本文件保存的位置(How to get Notepad file saved location)

编程入门 行业动态 更新时间:2024-10-25 20:24:02
如何获取记事本文件保存的位置(How to get Notepad file saved location)

如果保存在驱动器中,如何检索记事本文件的实际路径。 例如,记事本进程正在运行,它将保存在驱动器中的某个位置。 我该如何检索其完整路径? 使用下面的代码,我可以获得流程详细信息,但不能获取特定文件的实际路径。

Process[] localByName = Process.GetProcessesByName("notepad"); foreach (Process p in localByName) { string path = p.MainModule.FileName.ToString(); }

这将返回可执行路径,但我需要实际文件所在的驱动器位置。

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); }

仅当通过双击或通过命令行打开文件时,它才有效。

不要忘记通过右键单击“项目”,“添加引用”,然后选择“程序集”选项卡和“搜索系统管理”来添加对System.Management的引用。

步骤1

第2步

This should do it:

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.

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.

Step 1

Step 2

更多推荐

本文发布于:2023-07-16 12:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:记事本   位置   文件   Notepad   saved

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!