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