Advertisement
uwekeim

Split command line into file name and arguments

Sep 17th, 2014
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. /// <summary>
  2. /// Splits the file name if it contains an executable AND an argument.
  3. /// </summary>
  4. private static void checkSplitFileName( ref string fileName, ref string arguments )
  5. {
  6.     if ( !string.IsNullOrEmpty( fileName ) )
  7.     {
  8.         if ( fileName.IndexOf( @"http://" ) == 0 ||
  9.             fileName.IndexOf( @"https://" ) == 0 ||
  10.             fileName.IndexOf( @"ftp://" ) == 0 ||
  11.             fileName.IndexOf( @"file://" ) == 0 )
  12.         {
  13.             // URLs not supported, skip.
  14.             return;
  15.         }
  16.         if ( File.Exists( fileName ) )
  17.         {
  18.             // Already a full path, do nothing.
  19.             return;
  20.         }
  21.         else if ( Directory.Exists( fileName ) )
  22.         {
  23.             // Already a full path, do nothing.
  24.             return;
  25.         }
  26.         else
  27.         {
  28.             // Remember.
  29.             string originalFileName = fileName;
  30.  
  31.             if ( !string.IsNullOrEmpty( fileName ) )
  32.             {
  33.                 fileName = fileName.Trim();
  34.             }
  35.  
  36.             if ( !string.IsNullOrEmpty( arguments ) )
  37.             {
  38.                 arguments = arguments.Trim();
  39.             }
  40.  
  41.             // --
  42.  
  43.             if ( string.IsNullOrEmpty( arguments ) &&
  44.                 !string.IsNullOrEmpty( fileName ) && fileName.Length > 2 )
  45.             {
  46.                 if ( fileName.StartsWith( @"""" ) )
  47.                 {
  48.                     int pos = fileName.IndexOf( @"""", 1 );
  49.  
  50.                     if ( pos > 0 && fileName.Length > pos + 1 )
  51.                     {
  52.                         arguments = fileName.Substring( pos + 1 ).Trim();
  53.                         fileName = fileName.Substring( 0, pos + 1 ).Trim();
  54.                     }
  55.                 }
  56.                 else
  57.                 {
  58.                     int pos = fileName.IndexOf( @" " );
  59.                     if ( pos > 0 && fileName.Length > pos + 1 )
  60.                     {
  61.                         arguments = fileName.Substring( pos + 1 ).Trim();
  62.                         fileName = fileName.Substring( 0, pos + 1 ).Trim();
  63.                     }
  64.                 }
  65.             }
  66.  
  67.             // --
  68.             // Possibly revert back.
  69.  
  70.             if ( !string.IsNullOrEmpty( fileName ) )
  71.             {
  72.                 string s = fileName.Trim( '"' );
  73.                 if ( !File.Exists( s ) && !Directory.Exists( s ) )
  74.                 {
  75.                     fileName = originalFileName;
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement