none
What windows uses for evaluating paths with arguments RRS feed

  • Question

  • I am writing software in c# that validates the programs that are in autorun (particulary the registry), and I wanted to know if there is a way of finding out if a path is valid or not. I was looking on msdn and found PathGetArgs() and PathRemoveArgs(), however these have a problem...

    example:
    input: c:\program files\program\filename.exe
    path: c:\program
    arguments: files\program\filename.exe

    do you know of any other API's that are useful for this or is this it?

    Thanks!
    Friday, December 12, 2008 6:25 AM

Answers

  • To answer the OP's original question, Windows will begin at the first space and move through the string until it finds a valid executable.

    This is described in the MSDN page for CreateProcess. It's maintained for backwards compatibility reasons now.

    On a side note, placing a "program.exe" in the "c:\" directory is a fairly common hijacking trick used by some malware/viruses.

           -Steve
    • Marked as answer by ub3rst4r Sunday, December 14, 2008 5:00 AM
    Saturday, December 13, 2008 9:50 AM

All replies

  • So you want to see if some application executables can be found?

    If so, here it goes:

    string applicationPath = @"C:\Program Files\Internet Explorer\iexplore.exe"
    if(File.Exists(applicationPath)) 
         Console.WriteLine(applicationPath + " found."); 


    Tomi Airaksinen - MCPD [Remember to click "mark as answered" when you get a correct reply to your question]
    Friday, December 12, 2008 7:33 AM
  • Well, it is actually an incorrect entry.  The path should be quoted.  Pretty tough to find out where the path ends and the argument begins otherwise.  Does the shell actually execute this command properly?
    Hans Passant.
    Friday, December 12, 2008 2:14 PM
    Moderator
  • I concur with Tomi with using File.Exists (which is trivial) but I also agree with nobugz in regards that there are some difficulties with what "can" be inside those reg entries.

    I just took a look at my startups (used msconfig), and I found several variations that I believe you will have to handle:

    - Environment Variables, like %systemroot%
        - File.Exists("%systemroot%\\notepad.exe") is NOT a valid call and will return false
        - This means you'll have to detect the % and use Environment.GetEnvironmentVariable("systemroot") to get the path before using File.Exists
    - No full directory paths
        - File.Exists("notepad.exe") will also return false
        - You may have to get all the paths from the PATH environment variable and split on ; and check each one:
            - foreach(string path in Environment.GetEnvironmentVariable("PATH").split(';')
    - Parameters
        - What I've noticed is that if the reg entry's path will have double quotes around the path if there are spaces in it... but the parameters are outside of the double quotes, so that should be simple enough to parse

    I may be missing other types of scenarios to handle, but this is what I noticed with my startups.
    Please vote for my MIX 10K entry at: http://2009.visitmix.com/MIXtify/TenKDisplay.aspx?SubmissionID=0002
    Friday, December 12, 2008 4:54 PM
  • To answer the OP's original question, Windows will begin at the first space and move through the string until it finds a valid executable.

    This is described in the MSDN page for CreateProcess. It's maintained for backwards compatibility reasons now.

    On a side note, placing a "program.exe" in the "c:\" directory is a fairly common hijacking trick used by some malware/viruses.

           -Steve
    • Marked as answer by ub3rst4r Sunday, December 14, 2008 5:00 AM
    Saturday, December 13, 2008 9:50 AM