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.