locked
c# windows store app reading text file from Assets

    Question

  • Guys

    Trying to use following to read text from a file

                string FolderLocation = @"Assets\DATA.csv";
                StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                StorageFile sampleFile = await InstallationFolder.GetFileAsync(FolderLocation);
                string Lines = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
                string[] lines2 = Lines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                String t1 = temptext2.Text;
                int i = 10;
                bool failure = int.TryParse(t1, out i);
                List<string> names = lines2[i].Split(',').ToList<string>();
                names.Reverse();

    Keep getting a file not found error but the file "DATA.csv" is in the Assets Directory of my app

    any ideas where i'm going wrong?

    Mark

    Monday, February 2, 2015 4:27 PM

Answers

All replies

  • First, the build action of Data.csv need to be Content

    then

     ResourceContext rc = ResourceContext.GetForCurrentView();
     var map = ResourceManager.Current.MainResourceMap;

    var resource = map.GetValue("Files/Assets/DATA.csv", rc);

    StorageFile file = await resource.GetValueAsFileAsync();

    Monday, February 2, 2015 4:52 PM
  • Thanks Hereafter

    I added the code before mine but it says ResourceContext doesn't exist in the current context

    Mark


    • Edited by wambaugh1 Monday, February 2, 2015 5:05 PM
    Monday, February 2, 2015 5:04 PM
  • Hi Wambaugh1,

    As I know, that file has not been copied to install folder. You need to change the build action of that file to “Content”. Right click on the DataCsv file, choose properties and change build action like the following image.

    Then try your code again and let me know the result.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, February 11, 2015 2:39 AM
    Moderator
  • Hi wambaugh1,

    "doesn't exist in the current context" means that you don't have ResourceContext defined. There should be a red squiggle underneath it, and if you hover over it or if you hit Ctrl+. there will be a blue box which suggests how to fix it by either adding a "using Windows.ApplicationModel.Resources.Core;" statement to the top of the file or by explicitly referencing Windows.ApplicationModel.Resources.Core.ResourceContext.

    See http://channel9.msdn.com/Series/Windows-Store-Developer-Solutions/Visual-Studios-Red-Squigglies for a video discussing how to handle these errors.

    --Rob

    Wednesday, February 11, 2015 2:48 AM
    Owner