簡體   English   中英

如何將命令行參數傳遞給 WinForms 應用程序?

[英]How do I pass command-line arguments to a WinForms application?

我有兩個不同的 WinForms 應用程序,AppA 和 AppB。 兩者都運行.NET 2.0。

在 AppA 中,我想打開 AppB,但我需要將命令行參數傳遞給它。 如何使用我在命令行中傳遞的參數?

這是我目前在 AppB 中的主要方法,但我認為您無法更改它?

  static void main()
  {
  }

為您的 winforms 應用程序使用 args 的最佳方法是使用

string[] args = Environment.GetCommandLineArgs();

您可能可以將此與枚舉的使用結合起來,以鞏固整個代碼庫中數組的使用。

“而且你可以在應用程序的任何地方使用它,你不僅限於在 main() 方法中使用它,就像在控制台應用程序中一樣。”

發現於: 這里

static void Main(string[] args)
{
  // For the sake of this example, we're just printing the arguments to the console.
  for (int i = 0; i < args.Length; i++) {
    Console.WriteLine("args[{0}] == {1}", i, args[i]);
  }
}

然后參數將存儲在args字符串數組中:

$ AppB.exe firstArg secondArg thirdArg
args[0] == firstArg
args[1] == secondArg
args[2] == thirdArg

考慮您需要開發一個程序,您需要通過該程序傳遞兩個參數。 首先,您需要打開Program.cs類並在Main方法中添加參數,如下所示,並將這些參數傳遞給 Windows 窗體的構造函數。

static class Program
{    
   [STAThread]
   static void Main(string[] args)
   {            
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       Application.Run(new Form1(args[0], Convert.ToInt32(args[1])));           
   }
}

在 windows 窗體類中,添加一個參數化構造函數,它接受來自Program類的輸入值,如下所示。

public Form1(string s, int i)
{
    if (s != null && i > 0)
       MessageBox.Show(s + " " + i);
}

要對此進行測試,您可以打開命令提示符並轉到放置此 exe 的位置。 給出文件名,然后參數 1 參數 2。 例如,見下文

C:\MyApplication>Yourexename p10 5

從上面的 C# 代碼中,它會提示一個值為p10 5的消息框。

您可以通過訪問 Environment.CommandLine 屬性來獲取任何 .Net 應用程序的命令行。 它會將命令行作為單個字符串,但解析出您要查找的數據應該不會非常困難。

擁有一個空的 Main 方法不會影響此屬性或另一個程序添加命令行參數的能力。

您使用此簽名:(在 c# 中) static void Main(string[] args)

這篇文章也可能有助於解釋 main 函數在編程中的作用: http : //en.wikipedia.org/wiki/Main_function_( programming )

這是給你的一個小例子:

class Program
{
    static void Main(string[] args)
    {
        bool doSomething = false;

        if (args.Length > 0 && args[0].Equals("doSomething"))
            doSomething = true;

        if (doSomething) Console.WriteLine("Commandline parameter called");
    }
}

這可能不是每個人都喜歡的解決方案,但我喜歡 Visual Basic 中的應用程序框架,即使在使用 C# 時也是如此。

添加對Microsoft.VisualBasic的引用

創建一個名為 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);
    }

}

將 Main() 例程修改為如下所示

static class Program
{

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var application = new WindowsFormsApplication();
        application.Run(new Form1());
    }
}

此方法提供了一些額外的有用功能(如 SplashScreen 支持和一些有用的事件)

public event NetworkAvailableEventHandler NetworkAvailabilityChanged;d.
public event ShutdownEventHandler Shutdown;
public event StartupEventHandler Startup;
public event StartupNextInstanceEventHandler StartupNextInstance;
public event UnhandledExceptionEventHandler UnhandledException;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM