目录存在检查metro应用程序(不是文件)

时间:2015-01-21 07:49:01

标签: c# windows-store-apps windows-8.1

Directory.Exists(imgFolder.Path);

win8.1商店应用中的替代方法, 即时尝试在线搜索,但我确实得到文件的结果只存在不检查文件夹存在

1 个答案:

答案 0 :(得分:0)

在Windows 8.1中,您需要执行以下操作。

以下方法将检查文件是否存在:

 public async Task<bool> isFilePresent(string fileName)
 {
 bool fileExists = true;
 Stream fileStream = null;
 StorageFile file = null;

 try
 {
 file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
 fileStream = await file.OpenStreamForReadAsync();
 fileStream.Dispose();
 }
 catch (FileNotFoundException)
 {
 // If the file dosn't exits it throws an exception, make fileExists false in this case 
 fileExists = false;
 }
 finally
 {
 if (fileStream != null)
 {
 fileStream.Dispose();
 }
 }

 return fileExists;
 }

或:

public async Task<bool> isFilePresent(string fileName)
 {
 var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync(fileName);
 return item != null;
 }

来自Check If File Exists in Windows Phone 8 and Win RT

相关问题