2013-05-07 16 views
5

Mam około 10 metod w mojej klasie. W każdej metody używam ConfigurationManager.AppSettings dostać formularz wartość App.config złożyćConfigurationManager.AppSettings użyć innego pliku konfiguracyjnego

jak

_applicationPort = int.Parse(ConfigurationManager.AppSettings["ApplicationPort"] 

Mój problem jest, że chcę, aby ten kod uzyskać AppSettings z innego pliku app.config jak AnotherPoject.exe.config .

Odpowiedz

1

Możesz to zrobić, używając ConfigurationManager.OpenExeConfiguration. To pozwoli ci łatwo otworzyć inny plik konfiguracyjny.

MSDN artykuł o OpenExeConfiguration.

5

Można zrobić coś takiego

var fileConfig = ConfigurationManager.OpenExeConfiguration("<filePath>"); 
int port = int.Parse(fileConfig.AppSettings["PortNumber"].ToString()); 
+1

'fileConfig.AppSettings.Settings [" PortNumber "]. ToString()' jest tym, czego potrzebowałem – StingyJack

10

Można również ustawić app.config czytać kolejny plik. Coś takiego:

<?xml version="1.0"?> 
<configuration> 
    <appSettings file="my\custom\file\path\external.config"/> 
</configuration> 

i external.config będzie miał sekcję appSettings:

<appSettings> 
    <add key="myKey" value="myValue" /> 
</appSettings> 

odnieść do this msdn dodatkowych informacji.

Powiązane problemy