Поделиться через


App Config Data and Windows Store Apps

In traditional .NET applications developers rely on App.config and Web.config (client and server respectively) to store configuration data that the app depends on during runtime. Developers specify their config info in the <appsettings> section and use ConfiguationManager to access that info.

For Windows Store apps there isn't an exact equivalent for app.config and web.config. However, there are several mechanisms inherent to the Windows Runtime that developers can leverage to accomplish the same functionality.

LOCAL SETTINGSwith this API you can get the application settings container in the local app data store.

    1: var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    2:  
    3: // Create a simple setting
    4: localSettings.Values["exampleSetting"] = "Hello Windows";
    5:  
    6: // Read data from a simple setting
    7: Object value = localSettings.Values["exampleSetting"];
    8: if (!value)
    9: {
   10:     // No data
   11: }
   12: else
   13: {
   14:     // Access data in value
   15: }
   16:  
   17: // Delete a simple setting
   18: localSettings.Values.Remove("exampleSetting");

You can find the all detail about Local Settings in MSDN.

ROAMING SETTINGSwith this API you can get the application settings container in the roadming app data store.

    1: var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
    2:  
    3: // Create a simple setting
    4: roamingSettings.Values["exampleSetting"] = "Hello World";
    5:  
    6: // Read data from a simple setting
    7: Object value = roamingSettings.Values["exampleSetting"];
    8:  
    9: if (!value)
   10: {
   11:     // No data
   12: }
   13: else
   14: {
   15:     // Access data in value
   16: }
   17:  
   18: // Delete a simple setting
   19: roamingSettings.Values.Remove("exampleSetting");

You can find the all detail about Roaming Settings in MSDN.

PACKAGE.APPXMANIFEST - You can access file resources in app files that you deliver as part of the app package.

    1: async void ReadTimestamp()
    2: {
    3:    try
    4:    {
    5:       StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
    6:       String timestamp = await FileIO.ReadTextAsync(sampleFile);
    7:       // Data is contained in timestamp
    8:    }
    9:    catch (Exception)
   10:    {
   11:       // Timestamp not found
   12:    }
   13: }

Use the file APIs, such as Windows.Storage.StorageFolder.GetFileAsync,Windows.Storage.StorageFile.GetFileFromApplicationUriAsync and Windows.Storage.FileIO.ReadTextAsync to open and read a file in the local app data store.

LOAD DATA (XML, JASON) AT STARTUP – As a last resort you can HttpClient the configuration file on the fly when the application starts. This may be undesirable due to the connectivity dependency that isn't 100% guaranteed.


RE-IMAGINE EVERYTHING!

image

Oscars, Daytona 500, Seth MacFarlane, Putter ban, Fidel Castro, Erin Andrews, Streisand, Red carpet