0

When I pass the value from the OpenFilePicker() method back to the button click method, I can utilize a debug string and ensure that the value is not null.

However, when I pass it to the GetCellValue() method, a 'FileNotFound' exception is thrown. Utilizing a debug statement here also shows that the value is not null and returns a valid file path of "C:\Test.xlsx".

Tried changing file permissions to RWX for all, attempted different folder locations. All permissions and folders seem to have the same issue.

   public async void FileSelectButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            string filePath = await openFilePicker();
            //Debug.WriteLine("result:: " + filePath);
            GetCellValue(filePath, "Sheet1", "A1");
        }

        public async Task<string> openFilePicker()
        {
            var archerReportPicker = new 
            Windows.Storage.Pickers.FileOpenPicker(); 
            archerReportPicker.ViewMode = 
            Windows.Storage.Pickers.PickerViewMode.Thumbnail; 
            archerReportPicker.SuggestedStartLocation = 
            Windows.Storage.Pickers.PickerLocationId.Downloads; 
            archerReportPicker.FileTypeFilter.Add(".xlsx");
            archerReportPicker.FileTypeFilter.Add(".xls"); // Default extensions
            Windows.Storage.StorageFile archerReport = await archerReportPicker.PickSingleFileAsync(); //Get file
            if (archerReport != null)
            {
                // Application now has read/write access to the picked file
                this.fileTextBox.Text = archerReport.Name; // Load it up and throw the data in the textbox.
                var filePath = archerReport.Path;
                return filePath;
            }
            else
            {
                this.fileTextBox.Text = "";
                return null;
            }
        }

        public static string GetCellValue(string fileName, string sheetName, string addressName)
        {

            string value = null;

            // Open the spreadsheet document for read-only access.
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false)) //Line where exception is thrown
            {...}

Throws System.IO.FileNotFound Exception as opposed to opening valid file path.

The issue also occurs when filePath or fileName is defined using const string '@c:\test.xlsx'

AbsoZed
  • 27
  • 7
  • Prior to the `using` statement where the exception occurs, what is the literal value of `fileName`? Can you debug and show us the actual variable contents? – gravity Jun 25 '19 at 13:59
  • Sure. adding the code ```Debug.Writeline("result:: " + fileName)``` results in the _result:: C:\Test.xlsx_ being written to the debug console. – AbsoZed Jun 25 '19 at 14:02
  • 1
    Since openFilePicker returns Task you could call .Result like this: string filePath = await openFilePicker().Result; Keep in mind this will block the current thread. – Jake Steffen Jun 25 '19 at 14:03
  • @JakeSteffen, this causes an entirely different issue, stating that ```string does not contain a definition for GetAwaiter```. – AbsoZed Jun 25 '19 at 14:06
  • I think I saw that recently. Try putting the await openFilePicker() in its own variable before calling .Result on it like this: var tmpResult = await openFilePicker(); Also what version of .Net is the project targeting? – Jake Steffen Jun 25 '19 at 14:32
  • @JakeSteffen Utilizing this simply changes the error above thrown to a the new line where I call result on the temporary variable. I believe the error has to do with the return type of ``public async Task openFilePicker()```, not necessarily the typing of the variable. I'm unsure on .NET version, since it's a UWP app targeting 1809/1903, but I would assume 4.5. This information is not given in the normal place for UWP applications. – AbsoZed Jun 25 '19 at 14:39
  • Can you remove the async keyword from openFilePicker? I noticed that GetCellValue is not async. – Jake Steffen Jun 25 '19 at 14:53
  • @JakeSteffen, I cannot because to my knowledge that would require me to use ```PickSingleFileAndContinue()``` which is obsolete, and cannot be used, according to VS2019. – AbsoZed Jun 25 '19 at 14:56
  • It may be of note that even if I statically define the path with ```const string filePath = @"c:\test.xlsx"```, it throws the same exception. – AbsoZed Jun 25 '19 at 15:37

1 Answers1

0

The short answer to this question is here:

https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/

The gist of it is that in UWP, Storage Pickers return a non-filesystem bound Windows.Storage object. You can glean the filesystem path from the object, but because you are performing an operation on a secondary object, the fact that the user gave permissions for the first object does not apply to the second, resulting in an Access Denied condition when attempting to open the file - even if NTFS permissions allow 'Everyone' access.

This can be confirmed by monitoring the application using Process Monitor from SystemInternals.

If I discover a work-around to this issue, I will update this answer, but I will likely move away from UWP back towards a Windows Forms Application to avoid this issue entirely.

AbsoZed
  • 27
  • 7