AlexS
AlexS

Reputation: 520

File.Exists and SpreadsheetDocument.Open returning File Not Found Exception in UWP Application

Im having an issue with using DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open is not opening a spreadsheet, it returns a file not found exception. The class i'm using has worked many times before, but i've never used it in a UWP project.

I've created a simple example and found that I get the same issue when using File.Exists i've include all the using statements i use if that helps.

Does anyone know why the File.Exists cannot detect the file? and yes i've triple checked the file does exist on D:!

C# UWP Project created using UWP Template Studio [MainPage.xaml.cs]

using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;

using UWP_APP.ViewModels;
using Windows.UI.Xaml.Controls;

namespace UWP_APP.Views
{
    public sealed partial class MainPage : Page
    {
        public MainViewModel ViewModel { get; } = new MainViewModel();

        public MainPage()
        {
            InitializeComponent();

            string filePath = @"D:\example.xlsm";
            if (File.Exists(filePath))
            {
                int a = 1;
            }
            else
            {
                int a = 0;
            }

        }

Upvotes: 0

Views: 605

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

Does anyone know why the File.Exists cannot detect the file?

UWP app is running in sandbox, because File.Exists is System.IO api. So it could not work for accessing file except ApplicationData.Current.LocalFolder. If you do want to check if the file exist in the specific path, we suggest you add broadFileSystemAccess capability and enable in the system file access setting. This capability works for APIs in the Windows.Storage namespace.

And using the flolowing method to check if the file exist.

try
{
    var file = StorageFile.GetFileFromPathAsync(@"C:\Users\Karan\OneDrive\Desktop\2010.pdf");
    if (file != null)
    {
        isExist = true;
    }
}
catch (Exception)
{
    isExist = false;
}

Upvotes: 2

Related Questions