Environment...:. getcommandlineargs method:
Returns a string array containing the command line parameters of the current process.
Syntax:
Public static string [] getcommandlineargs ()
Return Value
Type: system. String []
String Array. Each element contains a command line parameter. The first element is the executable file name, followed by zero or more elements containing other command line parameters.
Note:
The first element in the array contains the name of the executable program. If the file name is unavailable, the first element is equal to string...:. Empty. Other elements include any additional markup entered in the command line.
The program file name can (but is not required) contain path information.
Command line parameters are separated by spaces. Double quotation marks (") can be used to include spaces in parameters. However, single quotes (') do not provide this function.
If two or even backslashes are followed by double quotation marks, each front backslash is replaced by a backslash and the double quotation marks are deleted. If an odd number (including only one) of backslashes is followed by double quotation marks, each of the front backslash pairs is replaced by a backslash, and the rest are deleted; however, in this case, double quotation marks are not deleted.
The following table shows how to separate command line parameters and assumes that MyApp is the currently executed application.
Input content on the command line |
Command line parameters generated |
MyApp Alpha Beta |
MyApp, alpha, beta |
MyApp "Alpha with spaces" "Beta with spaces" |
MyApp, alpha with spaces, Beta with spaces |
MyApp 'Alpha with spaces' Beta |
MyApp, 'Alpha, with, spaces', beta |
MyApp \ Alpha \ "Beta |
MyApp, \ Alpha, \ bet |
MyApp \\\\\ "Alpha \" Beta |
MyApp, \ "Alpha," Beta |
To obtain the command line as a single string, use the CommandLine attribute.
Example code:
Using system;
......
Using system. Windows. forms;
Namespace readdocument
{
Public partial class frmreaddocument: Form
{
Public frmreaddocument ()
{
Initializecomponent ();
}
Private void frmreaddocument_load (Object sender, eventargs E)
{
This. Location = new point (0, 0 );
// String [] cmdargs = environment. getcommandlineargs ();
// String strcmdargs = "";
// If (cmdargs. Count ()> 0)
//{
// Foreach (string STR in cmdargs)
//{
// Strcmdargs = strcmdargs + STR + "\ n ";
//}
//}
// MessageBox. Show (strcmdargs );
// Process = process. Start (STR, "\" "+ filename + "\"");
Try
{
String [] Arg = environment. getcommandlineargs ();
String argstrs = string. Join (",", ARG );
// MessageBox. Show ("argstrs:" + argstrs );
String [] argpath = argstrs. Split (",". tochararray ());
String filepath = argpath [1]. tostring ();
String strarr = "*. EXE:" + argpath [0]. tostring () + "\ r \ n" + "ARGs:" + argpath [1]. tostring ();
// MessageBox. Show (strarr );
This. webbrowser1.navigate (filepath, false );
This. Activate ();
}
Catch (exception ex)
{
Logs. writelog (ex. Message. tostring ());
}
}
Private void webbrowser1_previewkeydown (Object sender, previewkeydowneventargs E)
{
If (E. keycode = keys. Escape)
{
This. Close ();
This. Dispose ();
}
}
Private void webbrowserappsdocumentcompleted (Object sender, webbrowserdocumentcompletedeventargs E)
{
This. webbrowser1.visible = true;
}
Private void webbrowser1_navigating (Object sender, webbrowsernavigatingeventargs E)
{
This. backgroundimage = readdocument. properties. Resources. load;
Webbrowser1.visible = false;
}
Private void webbrowser1_newwindow (Object sender, canceleventargs E)
{
E. Cancel = true;
This. webbrowser1.navigate (system. Windows. Forms. webbrowser) sender). statustext, false );
}
Private void frmreaddocument_formclosed (Object sender, formclosedeventargs E)
{
This. Dispose ();
GC. Collect ();
GC. waitforpendingfinalizers ();
}
}
}