locked
FileOpenPicker problem on second use

    Question

  • In a Windows 8.1 project if I retrieve a file from local storage or if I use the FileOpenPicker to retrieve a file from the computer hard drive or OneDrive, everything works fine. If I then try to use the FileOpenPicker to retrieve another file I am presented with an Unauthorized Access error.

    Any ideas?


    Saturday, August 16, 2014 6:10 PM

Answers

  • Ok.  The problem is now solved by one line of code.

    In private async void OpenFile(), delay things a bit by inserting as a first line of code await System.Threading.Tasks.Task.Delay(100);

    I suppose that allows time for the message dialog to close before opening the FileOpenPicker to display.


    Monday, August 18, 2014 12:46 AM

All replies

  • Post some code
    Sunday, August 17, 2014 1:21 PM
  • Thanks for the response.

    You have prompted me to do what I should have done earlier.  I have created a simple test app with two buttons and two text blocks. Tap button 1 and a FileOpenPicker is brought up to select a text file.  Once selected that file's contents are placed in a text block.  Tap button 2 with the same result.  Everything works fine.  The code is below.

    So there is no problem with using the FilOpenPikker twice in succession. 

    But I do have a problem.  If after bringing in the text file, I read it line by line and use those line to create a List of things (people with first names, last names, birth date, etc.).  On first use, this is fine. Then on the second use of the FileOpenPikker to start the process, the app crashes with an unauthorized access error.  I am trying to access a different file from the first, so I don't understand the problem.

    private async void btn1_Click(object sender, RoutedEventArgs e)
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.ViewMode = PickerViewMode.List;
                openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                openPicker.FileTypeFilter.Add(".ged");

                StorageFile F = await openPicker.PickSingleFileAsync();  //produces an error when a second use

                var stream = await F.OpenReadAsync();
                var rdr = new StreamReader(stream.AsStreamForRead());
                string contents = rdr.ReadToEnd();
                rdr.Dispose();
                stream.Dispose();
                tb1.Text = contents;
            }

            private async void btn2_Click(object sender, RoutedEventArgs e)
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.ViewMode = PickerViewMode.List;
                openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                openPicker.FileTypeFilter.Add(".ged");

                StorageFile F = await openPicker.PickSingleFileAsync();  //produces an error when a second use          

                var stream = await F.OpenReadAsync();
                var rdr = new StreamReader(stream.AsStreamForRead());
                string contents = rdr.ReadToEnd();
                 rdr.Dispose();
                stream.Dispose();
                tb2.Text = contents;
            }

    Sunday, August 17, 2014 3:55 PM
  • Problem solved, sort of:

    On invoking the FileOpenPicker, I check to see if a file is already loaded and if so give the user the option to either load the new file or not.  This involves  a MessageDialog with Yes and  No buttons.  Apparently I have something wrong with the logic in the code of the Yes button because clicking on that actually produced two attempts (I think) to use the FileOpenPicker which triggered the crash.

    If I simply forget about the file that is open and go ahead and open another, everything works fine. 

    There seems to be a conflict between MessageDialogs with option buttons and the FileOpenPikcer

    Sunday, August 17, 2014 9:23 PM
  • Are you closing the file before reopening it ?

    If its still open you might get an error.


    n.Wright

    Sunday, August 17, 2014 10:47 PM
  • There are two different files involved.  But forget the two files.  I have narrowed the problem down to the interaction between a message dialog with option buttons that you produce before the FileOpenPicker. The code below demonstrates the problem.  Tap button2 and you are presented with the message dialog.  Select Yes and the code crashes at  StorageFile F =....

    I welcome a solution.  Thanks

     private async void btn2_Click(object sender, RoutedEventArgs e)
            {
                MessageDialog msgDialog = new MessageDialog("A file is already loaded. Are you sure you want to load a new file?", "Important");

                //OK Button
                UICommand okBtn2 = new UICommand("Yes");
                okBtn2.Invoked = YesBtn2Click;
                msgDialog.Commands.Add(okBtn2);

                //Cancel Button
                UICommand cancelBtn2 = new UICommand("No");
                cancelBtn2.Invoked = NoBtn2Click;
                msgDialog.Commands.Add(cancelBtn2);

                await msgDialog.ShowAsync();
     
            }

            private void NoBtn2Click(IUICommand command)
            {
                //do nothing
            }

            private void YesBtn2Click(IUICommand command)
            {
               OpenFile();            

            }

            private async void OpenFile()
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.ViewMode = PickerViewMode.List;
                openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                openPicker.FileTypeFilter.Add(".ged");

                StorageFile F = await openPicker.PickSingleFileAsync();  //produces an error when a second use          

                var stream = await F.OpenReadAsync();
                var rdr = new StreamReader(stream.AsStreamForRead());
                string contents = rdr.ReadToEnd();
                stream.Dispose();
                // tb2.Text = contents;
                StringReader sr = new StringReader(contents);
                string A = "***";
                string S = "";

                do
                {
                    A = sr.ReadLine();
                    S = S + Environment.NewLine + A;

                } while (A != "" && A != null);


                tb2.Text = S;
              
            }

    Monday, August 18, 2014 12:13 AM
  • Ok.  The problem is now solved by one line of code.

    In private async void OpenFile(), delay things a bit by inserting as a first line of code await System.Threading.Tasks.Task.Delay(100);

    I suppose that allows time for the message dialog to close before opening the FileOpenPicker to display.


    Monday, August 18, 2014 12:46 AM