esto puede no ser una solución popular para todo el mundo, pero me gusta el marco de aplicaciones en Visual Basic, incluso cuando se utiliza C#.
agregar una referencia a Microsoft.VisualBasic
Crear una clase llamada WindowsFormsApplication
public class WindowsFormsApplication : WindowsFormsApplicationBase
{
/// <summary>
/// Runs the specified mainForm in this application context.
/// </summary>
/// <param name="mainForm">Form that is run.</param>
public virtual void Run(Form mainForm)
{
// set up the main form.
this.MainForm = mainForm;
// Example code
((Form1)mainForm).FileName = this.CommandLineArgs[0];
// then, run the the main form.
this.Run(this.CommandLineArgs);
}
/// <summary>
/// Runs this.MainForm in this application context. Converts the command
/// line arguments correctly for the base this.Run method.
/// </summary>
/// <param name="commandLineArgs">Command line collection.</param>
private void Run(ReadOnlyCollection<string> commandLineArgs)
{
// convert the Collection<string> to string[], so that it can be used
// in the Run method.
ArrayList list = new ArrayList(commandLineArgs);
string[] commandLine = (string[])list.ToArray(typeof(string));
this.Run(commandLine);
}
}
Modificar su rutina principal() para tener este aspecto
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var application = new WindowsFormsApplication();
application.Run(new Form1());
}
}
Este método ofrece algunas características adicionales Utiles (como el soporte de SplashScreen y algunos eventos útiles)
public event NetworkAvailableEventHandler NetworkAvailabilityChanged;d.
public event ShutdownEventHandler Shutdown;
public event StartupEventHandler Startup;
public event StartupNextInstanceEventHandler StartupNextInstance;
public event UnhandledExceptionEventHandler UnhandledException;
entrada: "whatever.exe -v foo/lol nisp". Salida: args [0] = "-v"; args [1] = "foo"; args [2] = "/ lol"; args [3] = "nisp"; ¿Qué podría ser más fácil? –
@AlexeiLevenkov: ¡Muchas gracias! Alguien editó eso hace unos meses, y no me di cuenta de que estaba mal. Verificado con el código de ejemplo anterior que, de hecho, la primera entrada de la matriz es el primer argumento, y que el nombre del ejecutable no entra en la imagen. – Thomas
¡No puedo creer que vi tantas veces "string [] args" después de todo un año y nunca se me ocurrió que hasta ahora! jaja – Niklas