0

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:

enter image description here

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.

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • Never use exceptions to drive the flow of your code. A File.Exists before the call will avoid a lot of hassle. (not 100% safe but still....) – Steve Nov 25 '17 at 13:49

3 Answers3

2

You should check if the file exists unless it should always be there,e.g. Because it's part of your program. Nevertheless, you should use try catch around the whole thing because even if the file exists, it could be locked or a different reading error could occur.

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
2

Of the three solutions you proposed (ignore the error, check the file exists first, or catch the exception), only catching the exception will work. Ignoring the exception will let the app crash. Checking if the file exists before calling GetFileAsync has a timing issue where the file may be removed after the check but before opening it.

The fourth and best solution is to use StorageFile.TryGetItemAsync to return the file if it exists or null if it doesn't.

StorageFile file = await ApplicationData.Current.LocalFolder.TryGetItemAsync(uFileName) as StorageFile;
if (file != null)
{
    //...
}

The linked thread which says there's no way to check was correct for Windows Store apps in 2011. It's out of date for UWP apps in 2017.

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
1

You can either check the file exists before, or handle the exception.

When you don't catch the exception, the next line will not be executed so you can't check file for null (not like other programming language like C++).

The option, don't break on this option, only don't pause (activate a breakpoint) the application when exception is thrown only, doesn't change the behaviour of the program.

Daniel Tran
  • 6,083
  • 12
  • 25
  • 1
    I would like to add that checking if the file exists means to simply try accessing it: https://stackoverflow.com/questions/8626018/how-to-check-if-file-exists-in-a-windows-store-app , so try-catch is required in any case currently, I think. – tmighty Nov 25 '17 at 13:58
  • That's only applied to windows store app. You can check if a file exists or not by https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx – Daniel Tran Nov 25 '17 at 14:00
  • 1
    I'm dealing with UWP. You mean that when you say "Windows Store App", right? – tmighty Nov 25 '17 at 14:02
  • 1
    Sorry, I need to test and investigate a little before I accept. – tmighty Nov 25 '17 at 14:05
  • I have updated my question according to my investigations. I'm still dealing with the not-found-exception. – tmighty Nov 25 '17 at 14:14
  • So what's the remaining question? – Daniel Tran Nov 25 '17 at 14:17