0

My code is as follows: I used the File.Exists(path) but this doesn't seem to work. Would appreciate all help.

using System;
using System.IO;
private async void Search_Click(object sender, RoutedEventArgs e)
{
    String path = @"C:\Users\Karan\OneDrive\Desktop\2010.pdf";
    String t = Main(path);
    if (t=="1")
    {
        Test.Text = "1";
    }
    if (t=="0")
    {
        Test.Text = "0";
    }
}
public static string Main(String path)
{
    String t;
    if(File.Exists(path))
    {
        t = "1";
    }
    else
    {
        t = "0";
    }
    return t;
}

Thank you.

Leander
  • 854
  • 4
  • 17
KK29
  • 5
  • 2
  • Does this answer your question? [UWP Check If File Exists](https://stackoverflow.com/questions/37119464/uwp-check-if-file-exists) – Leander Oct 03 '20 at 11:06

1 Answers1

0

how do I locate a pdf file on clicking a button to check if a pdf file is present or not?

UWP app is running in sandbox, but 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 desktop, we suggest you add broadFileSystemAccess capability and enable in the system file access setting. This capability works for APIs in the Windows.Storage namespace.

try
{
    var file = StorageFile.GetFileFromPathAsync(@"C:\Users\Karan\OneDrive\Desktop\2010.pdf");
    if (file != null)
    {
        isExist = true;
    }
}
catch (Exception)
{
    isExist = false;
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36