locked
Can't access a text file in the solution explorer to read items into a list box

    Question

  • I'm trying to access a file in my application i have already created it and it's not in a folder in my solution explorer but i am trying to access it by pulling some words from the text file and then place it in a listbox when a button is pressed but nothing is happening can someone help me out with what might be going wrong i don't know if i'm not accessing the file in the right way but here is my code



    async private void txtFileBtn_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    // Get folder
                    Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                    // Get the file path from hardcoded string
                    StorageFile sampleFile = await localFolder.GetFileAsync("ks1words.txt");
                    // Read the lines
                    var words = await FileIO.ReadLinesAsync(sampleFile);
                    // Create a list
                    List<string> wordList = new List<string>(); 
                    // Loops and add word to list
                    foreach (var word in words) { wordList.Add(word); } // fill a listbox with the words 
                    // Set list as itemsource
                    lstBox.ItemsSource = wordList; 
    
                }
                catch { 
                    // Do nothing
                }  
    
            }
    Friday, April 18, 2014 6:55 PM

Answers

  • On what line do you get that error? I'm guessing that the words collection is empty on the return from FileIO.ReadLinesAsync, but there isn't enough detail to know for sure. If you step through this in the debugger and examine the returns from each function you should be able to narrow this down.

    Check in your app data folder on disk (you can see the path by looking at your localFolder variable in the debugger) to make sure that ks1words.txt exists (probably, since you didn't get a file not found error) and contains the data that you expect.

    --Rob

    Friday, April 18, 2014 8:46 PM
    Owner

All replies

  • If there is anything wrong then it should throw an exception. Your code tells that you are just suppressing the exception by not logging it anywhere. 

    Remove the try catch and then execute the application. Then if you see any exception then copy & paste the exception details here. That way it is helpful to us in understanding what exactly going wrong.

    Friday, April 18, 2014 7:22 PM
  • Here is the exception i recieved

    System.NullReferenceException was unhandled by user code
      HResult=-2147467261
      Message=Object reference not set to an instance of an object.
      Source=workingwtxtfiles
      StackTrace:
           at workingwtxtfiles.MainPage.<txtFileBtn_Click>d__0.MoveNext() in c:\Users\Tunde\Documents\Visual Studio 2012\Projects\workingwtxtfiles\workingwtxtfiles\MainPage.xaml.cs:line 48
      InnerException:

    Friday, April 18, 2014 7:49 PM
  • On what line do you get that error? I'm guessing that the words collection is empty on the return from FileIO.ReadLinesAsync, but there isn't enough detail to know for sure. If you step through this in the debugger and examine the returns from each function you should be able to narrow this down.

    Check in your app data folder on disk (you can see the path by looking at your localFolder variable in the debugger) to make sure that ks1words.txt exists (probably, since you didn't get a file not found error) and contains the data that you expect.

    --Rob

    Friday, April 18, 2014 8:46 PM
    Owner