I have the following code:
private async Task <string>IsolatedStorageReadTextFile(string uFileName)
{
string sRet = "";
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(uFileName);
if (file != null)
{
using (var inputStream = await file.OpenReadAsync())
using (var classicStream = inputStream.AsStreamForRead())
using (var streamReader = new StreamReader(classicStream))
{
while (streamReader.Peek() >= 0)
{
sRet = streamReader.ReadLine();
}
}
}
return sRet;
}
When the file in question doesn't exist, the IDE throws an error:
Should I
1) let the IDE debug warner ignore this error (say "Don't break on this exception"), and I should just let "if (file != null)" do the job
2) or should I check if the file actually exists
3) use try-catch?
I had to add an important part of code according to the answers:
private async Task <bool> LocalExists(string uFileName)
{
bool b = false;
//https://stackoverflow.com/questions/8626018/how-to-check-if-file-exists-in-a-windows-store-app
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(uFileName);
b = (file != null);
}
catch (Exception ex)
{
b = false;
}
return b;
}
This throws the same exception since in UWP, there seems no other way of checking if a file actually exists than trying to access it:
How to check if file exists in a Windows Store App?
So the question remains.