2009-07-24 133 views
71

我有兩個不同的WinForms應用程序,AppA & AppB。兩者都運行.NET 2.0。如何將命令行參數傳遞給WinForms應用程序?

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

這是我目前在AppB中的主要方法,但我不認爲你可以改變它?然後

static void main() 
    { 
    } 

回答

94
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 
+5

輸入:「whatever.exe -v foo/lol nisp」。輸出:args [0] =「-v」; args [1] =「foo」; args [2] =「/ lol」; args [3] =「nisp」;有什麼更容易? – 2009-07-24 19:22:45

+0

@AlexeiLevenkov:非常感謝!幾個月前有人編輯過,而我沒有意識到這是錯誤的。使用上面的示例代碼進行驗證,實際上,第一個數組條目是第一個參數,並且可執行文件的名稱不會輸入圖片。 – Thomas 2016-05-23 08:45:55

+0

不能相信我在一整年後看到'string [] args'這麼多次,它從來沒有發生過我現在一直到現在! haha – Niklas 2016-06-28 18:31:35

12

可以通過訪問Environment.CommandLine屬性抓住任何.NET應用程序的命令行。它將命令行作爲單個字符串,但解析出您正在查找的數據不應該非常困難。

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

7

您可以使用這個簽名:(在C#)靜態無效的主要(字串[] args)

這篇文章可能有助於解釋在編程的主要功能的作用,以及: 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"); 
    } 
} 
122

的最佳方式與args作爲你的WinForms應用程序的工作是使用

string[] args = Environment.GetCommandLineArgs(); 

您可以將其與使用enum結合使用以鞏固使用通過代碼庫的數組。

「你可以在你的應用程序中使用這個任何地方,你不只是 限制在一個控制檯應用程序 在main()方法使用它像。」

在這裏找到:HERE

5

這可能不是每個人的流行的解決方案,但我喜歡在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; 
5

考慮你需要開發一個程序,通過它你需要傳遞兩個參數。首先,您需要打開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])));   
    } 
} 

在窗口形式類中,添加從方案類接受的輸入值作爲像下方的參數的構造函數。

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

要測試此操作,可以打開命令提示符並轉到放置此exe文件的位置。給出文件名,然後給出parmeter1 parameter2。例如,見下面

C:\MyApplication>Yourexename p10 5 

從上面的C#代碼,它會提示一個MessageBox與值p10 5

相關問題