How to check if file exists in a Windows Store App?

前端 未结 10 2082
小鲜肉
小鲜肉 2020-11-30 07:25

Is there any other way of checking whether a file exists in a Windows Store app?

try
{
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync(         


        
10条回答
  •  天涯浪人
    2020-11-30 07:53

    I tried to write my own using old tricks:

    1. GetFileAttributesEx() always seems to end up with ERROR_ACCESS_DENIED if file selected via FileOpenPicker;
    2. Ditto for FindFirstFileEx();
    3. _stat() always ends up with ENOENT when file selected via FileOpenPicker;
    4. CreateFile2() with CREATE_NEW option works -- if file does exist it will fail with INVALID_HANDLE_VALUE return value and ERROR_FILE_EXISTS last error; if file does not exist you have to remember to delete created file afterwards.

    All in all -- you're better of sticking with exception handling method.

提交回复
热议问题