Finding a file path from a string

I get a list of bootable applications and I want to get only the Application Path launched at startup. The boot application list also contains a parameter passed to the application, which are in different templates; examples

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

I am trying to use the following regex

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

Please indicate to me how I can extract only the path to the program, ignoring all parameters and other launch commands.

+4
3

:

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);
+2

, .exe, . .Substring() String. :

 List<string> inputMessageStr = PopulateList(); // method that returns list of strings
 List<string> listofExePaths= inputMessageStr.Select(x=> x.Substring(0, x.IndexOf(".exe") + 4)).ToList();
+2

, . ".exe" . , .

. , . OP , , .

public string GetPathOnly(string strSource)
{
    //removing all the '"' double quote characters
    strSource.Trim( new Char[] {'"'} );

    int i;
    string strExecutablePath = "";
    for(i = 0; i < strSource.Length; ++i)
    {
        if(strSource[i] == ' ')
        {
            if(File.Exists(strExecutablePath))
            {
                return strExecutablePath;
            }               
        }
        strExecutablePath.Insert(strExecutablePath.Length, strSource[i]);
    }

    if(File.Exists(strExecutablePath))
    {
        return strExecutablePath;
    }

    return "";  // no actual executable path found.
}
+1
source

Source: https://habr.com/ru/post/1664965/


All Articles