locked
Command Line Arguments RRS feed

  • Question

  • Hi im working on my program and i was wondering if there is a way to handle command line arguments in c# on a windows form,

    for example if you type ccleaner /AUTO it automaticlly runs ____ cleaner 
    [>
    Saturday, August 16, 2008 4:36 PM

Answers

  • I am posting a routine that will do it although it may contain more stuff than you actually need. It loads a user profile and checks your security status and in case some executables need elevated priviledges this routine will proceed and the app will execute, otherwise nothing will happen. It of course concerns only some special folders. If you don't need it you can simplify the routine by removing those lines.

    The command parameter is the filename of the executable as a full directory path. The routine will break it down into the directory and filename. The parameters are  a separate entry. HostName (domain name) and the Username are global (static) variables in my setting. The password must be of SecureString class.

     
     
            private sting executeOtherAPI ( string command, string parameter )  
            {  
                // command is the name of excutable file like cmd.exe  
                string output = String.Empty;  
                string direct = string.Empty;  
                string fileName = string.Empty;  
                int indexer = command.LastIndexOf ( @"\" );  
                if ( indexer != -1 )  
                {  
                    direct = command.Substring ( 0, indexer + 1 );  
                    fileName = command.Substring ( indexer + 2, command.Length - indexer - 2 );  
                }  
                else  
                {  
                    return "Incorrect Input";;  
                }  
                SetControls.setAccessRule ( direct );  
                string action = command;  
                System.Diagnostics.Process proc = new Process ( );  
                ProcessStartInfo myProcessStartInfo = new ProcessStartInfo ( );  
                myProcessStartInfo.RedirectStandardInput = false;  
                myProcessStartInfo.UseShellExecute = false;  
                myProcessStartInfo.RedirectStandardOutput = true;  
                myProcessStartInfo.Arguments = parameter;  
                myProcessStartInfo.Domain = Globals.hostName;  
                myProcessStartInfo.UserName = Globals.userName;  
                myProcessStartInfo.Password = "*******";  
                myProcessStartInfo.CreateNoWindow = true;  
                myProcessStartInfo.FileName = fileName;  
                myProcessStartInfo.WorkingDirectory = direct;  
                proc.StartInfo = myProcessStartInfo;  
                try  
                {  
                    proc.Start ( );  
                    output = proc.StandardOutput.ReadToEnd ( );  
                    myProcessStartInfo.RedirectStandardOutput = false;  
                }  
                catch ( Exception ex )  
                {  
                    output = ex.Message;  
                    return output;  
                }  
                return output;  
            }                                               // executeOtherAPI        // Form1Add1  
     

    AlexB
    Saturday, August 16, 2008 5:12 PM