在UWP中,如何通过单击按钮来查找pdf文件,以检查是否存在pdf文件



我的代码如下:我使用了File.Exists(路径(,但这似乎不起作用。非常感谢您的帮助。

using System;
using System.IO;
private async void Search_Click(object sender, RoutedEventArgs e)
{
String path = @"C:UsersKaranOneDriveDesktop2010.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;
}

谢谢。

单击按钮检查是否存在pdf文件时,如何定位pdf文件?

UWP应用程序正在沙箱中运行,但File.Exists是System.IO api。所以它不适用于访问除ApplicationData.Current.LocalFolder之外的文件。如果您确实想检查桌面中是否存在该文件,我们建议您添加broadFileSystemAccess功能,并在系统文件访问设置中启用。此功能适用于Windows.Storage命名空间中的API。

try
{
var file = StorageFile.GetFileFromPathAsync(@"C:UsersKaranOneDriveDesktop2010.pdf");
if (file != null)
{
isExist = true;
}
}
catch (Exception)
{
isExist = false;
}

最新更新