C # _ winform receives command line parameters

Source: Internet
Author: User

First, I want to make a careful statement.Accept the command line parameters to makeProgramStart.Instead of starting the black box... Does the effect I want to achieve have something to do with it?

The Internet is the same, just Ctrl + C, CTRL + V, collectedArticleI have already suffered a lot and it has been a waste of time! BS ~~

first read this article: The winform program receives command line parameters. Let's take a look at the title... I think the same way, but I am very disappointed to read the content. However, since there are two API functions, test them first and add them to your favorites.

  ///  ///   Start the console  // /// 
    
    [ dllimport  (" kernel32.dll ")]  Public static extern bool  allocconsole (); ///  ///   release (close) console  // /// 
    
    [ dllimport  (" kernel32.dll ")]  Public static extern bool  freeconsole (); 

 

You can use this method to call the black box (console) in winform. Specific use... Practice it yourself...

Now let's get to the point and explain the functions I want to implement:

For example, in cmd.exe, we enter shutdown. This command can be used to restart the system, shut down the system, and so on. It has a series of optional parameters:

We can find the EXE Executable File shutdown.exe under C: \ windows \ system32. This program accepts command line parameters. When the parameters meet a built-in parameter, it executes an operation.

We want to implement this function! Such a program can be used in any project.System. Diagnostics. process. Start ("your program .exe", "parameter 1Parameter 2").

In general, each project has a main function, which is the entry point of the entire program, so the parameters must be passed here!

Default main function:

  ///  ///   main entry point of the application.   //   [ stathread ]  static void  main () { application  .  enablevisualstyles ();  application  .  setcompatibletextrenderingdefault ( false );  application  .  Run ( New   form1  () ;}

We just need to change it to this:

 /// <Summary> ///  The main entry point of the application.  /// </Summary> [ Stathread ] Static void Main ( String [] ARGs ){ Application  . Enablevisualstyles (); Application . Setcompatibletextrenderingdefault ( False ); If (ARGs . Length =  0 ) Application  . Run ( New  Form1 ()); Else  Application  . Run ( New Form1 (ARGs ));}

Form1 form construction:

 
String[] ARGs=Null;PublicForm1 () {initializecomponent ();}PublicForm1 (String[] ARGs) {initializecomponent ();This.ARGs=ARGs ;}

it's really a few simple lines of Code ... We have the ARGs parameter and the remaining code... Let's look at your needs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.