how to check whether file exists or not in a metro app using winjs? RRS feed

  • Question

  • I am building an metro app using javascript. my requirement goes like this.

    I will retrieve a name on runtime. I will add suffix(.png) to it and will check whether the image exists or not?

    any pointers on how to do the same.


    Tuesday, August 28, 2012 4:27 AM


All replies

  • What are you doing with the file?  If you want to create it if it doesn't exist you can add create flags.  You can try to open it and trap the error if it doesn't exist.

    Start with this sample to understand how to use the Storage namespace: http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    Jeff Sanders (MSFT)

    Tuesday, August 28, 2012 12:19 PM
  • Can you not just post example code as i cannot find any anywhere...

    I need to check image exists before i put it in listview.

    Tuesday, November 13, 2012 1:32 PM
  • Hi Ashley,

    Look at this function in the sample:  validateFileExistence

    Also take the time to understand this sample, it will help you to understand file management and storage and you will then be able to apply it to your application to make it a great Windows Store app.


    Jeff Sanders (MSFT)

    Thursday, November 15, 2012 1:27 PM
  • Hello,
    you can adapt following code to your case

    //check if localFile.txt exists in local data folder
      function (found) {
      //What I whant to do
    I hope that it will help someone ;)

    • Proposed as answer by Camer Boy Friday, May 10, 2013 3:13 AM
    • Unproposed as answer by Camer Boy Friday, May 10, 2013 3:13 AM
    • Proposed as answer by Camer Boy Friday, May 10, 2013 3:13 AM
    Friday, May 10, 2013 3:13 AM
  • In any case, this does not check if a file exists, but if your app can access it. Whether a file exists or you do not have permission to read/write to it, WinRT will always (to prevent probing my malware) throw an Access Denied exception and never a "File Not Found" exception.
    Friday, May 10, 2013 12:31 PM