0

I am working on a project where values are stored in a Text file and should be retrieved every time the application starts. The text file is very simple and straight forward:

{
    "HowManyDayImages": "11",
    "HowManyNightImages": "5",
}

I have this simple class with two values as numbers:

public class ImageConfig
{
    public int HowManyDayImages { get; set; }
    public int HowManyNightImages { get; set; }
}

then I have this code to look up the respected file and read the values from it and set them:

public static async void LoadConfigAsync()
{
    try
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
        string themeName = (string)settings.Values["Theme"];
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        StorageFolder timeFolder = await localFolder.GetFolderAsync("Dynamic");
        StorageFolder themeFolder = await localFolder.GetFolderAsync(themeName);
        string imageConf;
        if (File.Exists("images.conf"))
        {
            imageConf = File.ReadAllText("images.conf");
        }
        else
        {
            imageConf = Encoding.UTF8.GetString(Properties.Resources.imageConf);
        }
        imageSettings = JsonConvert.DeserializeObject<ImageConfig>(imageConf);
    }
    catch
    {
    }
}

with this I am facing two problems. the first is that in this line " Encoding.UTF8.GetString(Properties.Resources.imageConf);" I am getting the error that "Properties" does not exist in the current context.

The Dynamic folder inside the project

The second problem I am facing is that I keep getting an error saying that "Dynamic" could not be found.

WynDiesel
  • 1,104
  • 7
  • 38
Asem Khen
  • 317
  • 3
  • 15
  • Is there any reason why you're not using appconfig/webconfig? – WynDiesel Jul 23 '18 at 12:12
  • Well I am just starting to learn. to me it seemed a bit easier using local settings. I am trying to work out the appconfig, and if it worked out I will start using it – Asem Khen Jul 23 '18 at 12:19
  • This is what you get by copy/pasting any code found on internet. Take a step-by-step tutorial to learn everything from beginning. – Renatas M. Jul 23 '18 at 12:19
  • 2
    @AsemKhen, take a look at https://stackoverflow.com/questions/13043530/what-is-app-config-in-c-net-how-to-use-it. It's a way to read text setting's from a file, at run time, that's an industry standard that's been tried and tested. No need to re-invent the wheel! – WynDiesel Jul 23 '18 at 12:21
  • 1
    @WynDiesel, App.config seems to be really helpful to store application settings within the apps directory. What I am trying to reach is reading an external configuration files bundled in folders that can be added or deleted. ( The app looks up a Zip file, extracts it to an internal folder and reads the configuration within it to set the previously mentioned values ) – Asem Khen Jul 23 '18 at 12:59
  • 1
    @AsemKhen I think Wyn was referring to the use of Properties to hold the name of the file, as opposed to storing the filename in app.config. – ADyson Jul 23 '18 at 13:09

1 Answers1

4

How to correctly read values from a Text File

First you need a text file that stored json info, then store it in specified folder that you could access next time.

Create text file

// Create sample file; replace if exists.
Windows.Storage.StorageFolder storageFolder =
    Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile =
    await storageFolder.CreateFileAsync("sample.txt",
        Windows.Storage.CreationCollisionOption.ReplaceExisting);

Write text to file

Windows.Storage.StorageFolder storageFolder =
    Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile =
    await storageFolder.GetFileAsync("sample.txt");

await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "json info");

Second load the text from the text file, then use JsonConvert to deserialize the text.

Reading from a file

string json = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);

var item = JsonConvert.DeserializeObject<Item>(json);

Third load the setting from DeserializeObject that get from the above step.

var info = item.info;

Note

The second problem I am facing is that I keep getting an error saying that "Dynamic" could not be found.

From you screen shot, the Dynamic folder stored under your project directory, you could not use LocalFolder to get it. Please use package InstalledLocation.

var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Dyanmic");
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36