2011-06-02 65 views

回答

6
在你的程序的 Main方法

args字符串數組參數的方法將包含任何命令行參數。 args數組將包含1個值對於未封閉在引號(「)

每個空間分離的元件,從而

myprograme.exe c:\my documents\file1.xls 

將導致2個ARGS:

c:\my 
documents\file1.xls 

myprograme.exe "c:\my documents\file1.xls" 

將導致參數值爲1:

c:\my documents\file1.xls 

你可以通過索引訪問PARAMS:

string file = args[0]; 

假設該文件是第一個參數。

顯然你仍然需要加載實際的文件,這隻會給你的名字作爲參數給你的程序。

+0

這很好!謝謝! – buzzzzjay 2011-06-02 19:50:10

+1

沒問題,很高興幫助。如果這回答您的問題,您應該點擊問題左側的投票按鈕下方的勾號,將其標記爲接受的答案。 – 2011-06-02 20:09:55

4

您可以使用args [0]檢索文件。

public static void Main(string [] args) 
{ 
    //This will print the first argument you passed in on command line. 
    Console.WriteLine(args[0]); 
}