C#_winform receiving command-line arguments

Source: Internet
Author: User


c#_winform receiving command-line arguments2014-08-03 10:17 534 People read comments (0) favorite reports


First of all, I would like to make a careful statement that this article is about accepting command line arguments and letting the program start. instead of starting that black box ... The effect I'm trying to achieve is related to it.



Online those stereotyped, just ctrl+c,ctrl+v, collected articles, I have been deeply hurt, wasted a lot of time! bs~~






Let's take a look at this article: theWinForm program receives command-line arguments . A look at the title, uh ... Like my idea, but a look at the content, greatly disappointed. However, since there are 2 API functions, the first Test, a collection.


 
/ / / < summary >
/// start the console
/ / / < / summary >
/ / / < returns > < / returns >
[DllImport (" kernel32. DLL)]
Public static extern bool AllocConsole();
/ / / < summary >
/// release (close) the console
/ / / < / summary >
/ / / < returns > < / returns >
[DllImport (" kernel32. DLL)]
Public static extern bool FreeConsole();





The black box (console) is called in WinForm, which can be implemented with this. Specific use of ... Practice It yourself ...



Now, let's talk about the features I want to achieve:



For example, under Cmd.exe, we enter shutdown, which can be used to reboot the system, shut down the system, and so on. It has a range of parameters to choose from:






We can find Shutdown.exe this exe executable file under C:\Windows\System32\. This program accepts command-line arguments and performs an action when the parameter conforms to a built-in parameter.



That's what we're trying to achieve! Such a program can be used in any project, via System.Diagnostics.Process.Start ("your program. EXE parameter 1").



In general, each project has a main function, which is the entry point of the entire program, then the parameters are definitely passed here!



The default main function:


 
/ / / < summary >
/// the main entry point for the application.
/ / / < / summary >
[STAThread]
The static void Main ()
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false);
Application. The Run (new _click ());
}


We just need to change to this:


 
/ / / < summary >
/// the main entry point for the application.
/ / / < / summary >
[STAThread]
The static void Main (string [] args)
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false);
If (args. The Length = = 0)
Application. The Run (new _click ());
The else
Application. The Run (new _click (args));
}


form1 form:


 
string[] args=null;
public Form1()
{
    InitializeComponent();
}
public Form1(string[] args)
{
    InitializeComponent();
    this.args = args;
}


Really is very simple very simple several lines of code ... We have the args parameter, the rest of the code ... Look at your needs.



C#_winform receive command-line arguments


Related Article

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.