直起docs:
// Get the AppSettings section.
// This function uses the AppSettings property
// to read the appSettings configuration
// section.
public static void ReadAppSettings()
{
try
{
// Get the AppSettings section.
NameValueCollection appSettings =
ConfigurationManager.AppSettings;
// Get the AppSettings section elements.
Console.WriteLine();
Console.WriteLine("Using AppSettings property.");
Console.WriteLine("Application settings:");
if (appSettings.Count == 0)
{
Console.WriteLine("[ReadAppSettings: {0}]",
"AppSettings is empty Use GetSection command first.");
}
for (int i = 0; i < appSettings.Count; i++)
{
Console.WriteLine("#{0} Key: {1} Value: {2}",
i, appSettings.GetKey(i), appSettings[i]);
}
}
catch (ConfigurationErrorsException e)
{
Console.WriteLine("[ReadAppSettings: {0}]",
e.ToString());
}
}
所以,如果你想訪問設置Scenario1.doc
,你可以這樣做:
var value = ConfigurationManager.AppSettings["Scenario1.doc"];
編輯:
由於加布裏埃爾GM在評論中表示,你將不得不添加對System.Configuration
的引用。
我正在使用.NET 4.0 –
我發佈的作品是2.0 ** + **。 – Gromer
如果您沒有看到'ConfigurationManager',請嘗試將'system.configuration'添加到您的項目引用中。 –