2010-06-06 26 views
1

我需要一個採樣/例子來展示如何通過一個配置文件作爲淨如何配置文件作爲參數傳遞給控制檯應用程序的.Net

參數的控制檯應用程序
+0

什麼樣的配置文件?你的意思是ProgramName.exe.config文件?爲什麼不把文件名作爲參數傳遞?你嘗試了什麼?你有什麼問題? – 2010-06-06 18:56:59

+0

該文件將具有像FileOutputDirectory等配置設置,它可以位於任何物理位置,如C:\ file.config – Sandhurst 2010-06-06 18:59:15

回答

1

通過它的命令行參數,在args[]。 就是這樣。

class Program 
{ 
    static void Main(string[] args) 
    { 
     if (args == null) 
     { 
      Console.WriteLine("args is null."); // Check for null array 
     } 
     else 
     { 
      // use args to get passed config file path 
     } 
    } 
} 

~~~如何調用該程序~~~

C:\ ConsoleApplication1.exe中 「你的配置 文件路徑」(如C:\設置\ app.config)中

0

您是否有權訪問目標控制檯應用程序源代碼?它是.NET應用程序嗎?

如果是的話下一步:添加目標應用程序作爲源應用程序項目的引用(exe可以像DLL一樣添加,沒有區別)。並調用一些公開的方法。

// target.exe code 
namespace Target { 
    public class MyConfig { } 

    public class Program { 
     static void Main(string[] args) { } 
     public static void EntryPoint(MyConfig conf) { } 
    } 
} 

// source.exe code 
namespace Source { 
    class Program { 
     static void Main(string[] args) { 
     Target.MyConfig conf = new Target.Config(); 
     Target.Program.EntryPoint(conf); 
     } 
    } 
} 
0

如果你想存儲像FileOutputDirectory這樣的數據,你也可以使用設置頁面而不是配置文件。設置頁面很容易配置和使用。閱讀更多在msdn網站:link text

相關問題