2012-02-13 72 views
0

获取可执行文件的名称。当我从注册表得到一个完整的命令行字符串,或其他地方,例如:从完整的命令行

mycommand -p -o c:\file\1.txt -r output 

"c:\program files\dir\executable.exe" -options -o1 -o2 

我怎么能轻松地之间的分裂可执行文件和参数?

感谢

回答

3

Windows命令行解析是相当靠不住的,因为它是由C运行时库完成的(你可以检查代码在Visual Studio中的东西安装目录像

  • C:\ Program Files文件(x86)的\微软的Visual Studio 10.0 \ VC \ CRT \ SRC \ stdargv.c

你的机器上的实际路径可能会有所不同,当然。而且,如果您没有使用Visual Studio安装C运行时源代码,它将不会是他们的。

我相信这样的,因为它是从DOS继承的,所以它是相当的这些混沌的“逻辑”。

的基本语法是这样的:

  • A命令行是由空格分隔1个或多个字的序列。

  • 每个字是1或多个以下的序列:BARE_WORD,QUOTED_WORD或ESCAPE_SEQUENCE。单词由空白或命令行结尾终止。

  • 甲BARE_WORD比反斜杠( '\')其它1个或多个字符,双引号(序列' “ ')或空白。

  • 甲QUOTED_WORD由LEAD_IN_QUOTE引入(”' “),接着是零个或多个以下:

    • 空白
    • ESCAPE_SEQUENCE
    • 裸词

    并由LEAD_OUT_QUOTE('“')终止。导入和导出报价从引用的单词中删除。

  • 一个ESCAPE_SEQUENCE是下列结构之一:

    • 偶数个反斜杠( ' “ '),后跟一个引号('”')。
      这代表了一系列逃逸的反斜杠,然后是导入/导出报价。每对反斜杠代表一个反斜杠。
    • 奇数个反斜杠,后跟一个引号(''')。
      这表示一系列转义反斜杠,后面跟着一个文字引号。
    • 反斜杠序列,后面跟着一个引号。
      这表示一系列未转义的反斜杠,并按原样传递。

这就是它。

命令行中的第一个单词是命令名称(例如,可执行文件的名称/路径)。严格地说,解析命令名称应该比其他词语更简单,因为它必须表示有效的NTFS文件名称。然而,这不一定是真实的,取决于谁组成命令行。

下面是一些示例C#代码,应该以与Windows操作系统相同的方式解析任何给定的命令行,尽管我应该注意,这有而不是经过了全面测试。

方法Parse()返回IEnumerable<string>,其中第一个元素是命令/程序名称,其余部分是组成参数的单词。

class CommandLineParser 
{ 
    char[]  cmd; // source buffer 
    StringBuilder buf; // output buffer 
    int   i; // current position within the source buffer 

    public CommandLineParser() 
    { 
     cmd = null; 
     buf = null; 
     i = -1; 
     return; 
    } 

    public IEnumerable<string> Parse(string commandLine) 
    { 
     cmd = commandLine.ToCharArray(); 
     buf = new StringBuilder(); 
     i = 0; 

     while (i < cmd.Length) 
     { 
      char ch = cmd[i]; 

      if (char.IsWhiteSpace(ch)) { throw new InvalidOperationException(); } 
      else if (ch == '\\') { ParseEscapeSequence(); } 
      else if (ch == '"') { ParseQuotedWord(); } 
      else { ParseBareWord(); } 

      if (i >= cmd.Length || char.IsWhiteSpace(cmd[i])) 
      { 
       string arg = buf.ToString(); 

       yield return arg; 

       buf.Length = 0; 
       ConsumeWhitespace();      
      }     
     }    
    } 

    /// <summary> 
    /// Parse a quoted word 
    /// </summary> 
    private void ParseQuotedWord() 
    { 

     // scan over the lead-in quotation mark w/o adding it to the buffer 
     ++i; 

     // scan the contents of the quoted word into the buffer 
     while (i < cmd.Length && cmd[i] != '"') 
     { 
      char ch = cmd[i]; 
      if (ch == '\\') { ParseEscapeSequence(); } 
      else { buf.Append(ch); ++i; } 
     } 

     // scan over the lead-out quotation mark w/o adding it to the buffer 
     if (i < cmd.Length) 
     { 
      ++i; 
     }    
     return; 
    } 

    /// <summary> 
    /// Parse a bareword 
    /// </summary> 
    private void ParseBareWord() 
    {    
     while (i < cmd.Length) 
     { 
      char ch = cmd[i]; 
      if (char.IsWhiteSpace(ch)) break; // whitespace terminates a bareword 
      else if (ch == '"') break; // lead-in quote starts a quoted word 
      else if (ch == '\\') break; // escape sequence terminates the bareword 

      buf.Append(ch); // otherwise, keep reading this word     

      ++i;     
     }    
     return; 
    } 

    /// <summary> 
    /// Parse an escape sequence of one or more backslashes followed an an optional trailing quotation mark 
    /// </summary> 
    private void ParseEscapeSequence() 
    { 
     //--------------------------------------------------------------------------------------------------------- 
     // The rule is that: 
     // 
     // * An even number of backslashes followed by a quotation mark ('"') means that 
     // - the backslashes are escaped, so half that many get injected into the buffer, and 
     // - the quotation mark is a lead-in/lead-out quotation mark that marks the start of a quoted word 
     //  which does not get added to the buffer. 
     // 
     // * An odd number of backslashes followed by a quotation mark ('"') means that 
     // - the backslashes are escaped, so half that many get injected into the buffer, and 
     // - the quotation mark is escaped. It's a literal quotation mark that also gets injected into the buffer 
     // 
     // * Any number of backslashes that aren't followed by a quotation mark ('"') have no special meaning: 
     // all of them get added to the buffer as-sis. 
     // 
     //--------------------------------------------------------------------------------------------------------- 

     // 
     // scan in the backslashes 
     // 
     int p = i; // start of the escape sequence 
     while (i < cmd.Length && cmd[i] == '\\') 
     { 
      buf.Append('\\'); 
      ++i; 
     } 

     // 
     // if the backslash sequence is followed by a quotation mark, it's an escape sequence 
     // 
     if (i < cmd.Length && cmd[i] == '"') 
     { 
      int n   = (i - p); // find the number of backslashes seen 
      int quotient = n >> 1; // n divide 2 (5 div 2 = 2 , 6 div 2 = 3) 
      int remainder = n & 1; // n modulo 2 (5 mod 2 = 1 , 6 mod 2 = 0) 

      buf.Length -= (quotient + remainder); // remove the unwanted backslashes 

      if (remainder != 0) 
      { 
       // the trailing quotation mark is an escaped, literal quotation mark 
       // add it to the buffer and increment the pointer 
       buf.Append('"'); 
       ++i; 
      }     
     }    
     return; 
    } 

    /// <summary> 
    /// Consume inter-argument whitespace 
    /// </summary> 
    private void ConsumeWhitespace() 
    { 
     while (i < cmd.Length && char.IsWhiteSpace(cmd[i])) 
     { 
      ++i; 
     } 
     return; 
    }   
} 

class Program 
{ 
    static void Main() 
    { 
     CommandLineParser parser  = new CommandLineParser(); 
     string   commandLine = RetrieveUnparsedCommandLine(); 
     int i = 0; 

     IEnumerable<string> args = parser.Parse(commandLine); 
     Console.WriteLine("-------------------"); 
     foreach (string arg in args) 
     { 
      string template = i > 0 ? "argv[0:#0]" : "command"; 
      string label = string.Format(template , i++); 

      Console.WriteLine("{0}: {1}" , label , arg); 

     } 
     Console.WriteLine("-------------------------");    
     return; 
    } 

    static string RetrieveUnparsedCommandLine() 
    { 
     // get the raw command line. Source might be registry, config file, whatever 
     string commandLine = Environment.CommandLine; 
     return commandLine; 
    }  
} 

祝你好运。

1

由于其中包含了可能有效的命令行输入的字符串:

static string[] SplitArgs(string input) 
{ 
    var args = new List<string>(); 
    var parts = input.Split(' '); 

    for (int ii = 0; ii < parts.Length; ++ii) 
    { 
     // if it starts with a quote, search to the end 
     // NB: this does not handle the case of --x="hello world" 
     // an arguments post processor is required in that case 
     if (parts[ii].StartsWith("\"")) 
     { 
      var builder = new StringBuilder(parts[ii].Substring(0)); 
      while (ii + 1 < parts.Length 
       && !parts[++ii].EndsWith("\"")) 
      { 
       builder.Append(' '); 
      } 

      // if we made it here before the end of the string 
      // it is the end of a quoted argument 
      if (ii < parts.Length) 
       builder.Append(parts[ii].Substring(0, parts[ii].Length - 1)); 

      args.Add(builder.ToString()); 
     } 
     else 
      args.Add(part[ii]); 
    } 

    return args.ToArray(); 
} 
+0

对不起,但我需要一个通用的方法来做到这一点,我的应用程序不是一个ConsoleApplication – 2012-02-13 19:18:25

+0

你可以展开你的意思,它是一个WinForms,WPF等?或者它是一个给定一个字符串或一个字符串数组的库? – user7116 2012-02-13 19:20:40

+0

我从用户,文件中获取字符串,这是完整的命令行,我需要区分可执行文件和参数。 – 2012-02-13 19:22:47