c# - 从字符串中查找文件路径

标签 c#

我正在获取启动应用程序列表,并且只想获取启动时运行的应用程序的路径。启动应用程序列表还包含传递给应用程序的参数,这些参数有不同的模式;例子是

C:\Program Files (x86)\Internet Download Manager\IDMan.exe /onboot

"C:\Program Files\Process Hacker 2\ProcessHacker.exe" -hide

"C:\Program Files\CCleaner\CCleaner64.exe" /MONITOR

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --no-startup-window /prefetch:5

"C:\Program Files (x86)\GlassWire\glasswire.exe" -hide

C:\Program Files\IDT\WDM\sttray64.exe

我正在尝试使用以下正则表达式

Regex.Matches(input, "([a-zA-Z]*:[\\[a-zA-Z0-9 .]*]*)");

请指导我如何只提取应用程序路径而忽略所有参数和其他启动命令。

最佳答案

试试这个简单的方法:

string cmd = "\"C:\\Program Files (x86)\\GlassWire\\glasswire.exe\" -hide";

int index = cmd.ToLower().LastIndexOf(".exe");
string path = cmd.Substring(0, index+4);
index = path.IndexOf("\"");
if (index >= 0)
path = path.Substring(index + 1);

关于c# - 从字符串中查找文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41339733/

相关文章:

c# - "this"如何在C#中传递

c# - 如何在 C# 中计算这种类型的方程 (x^1+...x^n)?

c# - 静态构造函数不适用于结构

C# 字符串相等运算符返回 false,但我很确定它应该是 true...什么?

c# - 无法单击属性页 visual studio 2012

c# - X509Certificate2 在 Linux 上解析失败但在 Windows 上有效

c# - 如何使用依赖注入(inject)将状态传递到依赖链中

c# - 将缺失的月份计数为零 (0) 并填充到数据表中

c# - 通过慢速网络下载大文件时出现 StorageException

c# - 如何使用 bool 变量切换 EntityFramework Tracker