locked
Still no way to check for a file?

    Question

  • Hello guys.

    Is there any way for us to check for the existence of a file (without trying to open it and passing an erro function to the returned promise)?

    thanks.


    Luis Abreu

    Sunday, March 18, 2012 2:56 PM

Answers

All replies

  • I think no at this moment. The reply in http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/1eb71a80-c59c-4146-aeb6-fefd69f4b4bb is still the best solution.

    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 19, 2012 8:35 AM
  • Hi Luis, 

    An "ifExists" method would simply add one more async method to your logic. Attempting to access the file is a one-step operation that will either return you the file or notify you that it doesn't exist.

    Justin Cooperman

    Thursday, March 22, 2012 7:02 PM
  • Luis,

    If WinRT returned null to the completion handle for opening a file that doesn't exist instead of an error, would that be more helpful than triggering the error handler? This is something I've been thinking about for a while and it is a pattern that shows up in other places in WinRT (such as someone cancelling signing into their Live account.)

    Cheers,

    -Jeff


    Thursday, March 22, 2012 8:21 PM
  • Hello guys.

    sure, but that would let me write the code without putting a "huge" try/catch block around it. And that try/catch block might even catch not related exceptions, right?

    And yes Jeff, probably receiving null would help in read scenarios (that would let me skip the rest of my code...)


    Luis Abreu

    Friday, March 23, 2012 9:52 AM
  • In JS, no need for try/catch:

    Windows.Storage.KnownFolders.picturesLibrary.getFileAsync("myFile.jpg").done(
                function (file) {
                    // Use "file"
                }, function (error) {
                    // Handle "error"
    });

    Friday, March 23, 2012 11:01 PM
  • Just get the contents of the folder the file is in and check if the file exists with a "contains" call.

    No try/catch, no errors or exceptions.


    Life Is...

    Friday, December 07, 2012 1:02 AM