Search code examples
javascriptapisdkwindows-8launcher

Windows.System.Launcher.launchFileAsync(file) not working :(


I was trying to work with JS API for Windows 8 App Store Applications. Basically I have two buttons, one invoking the function create() and one the function open(). The file is correctly created, but I can't imagine how to launch an external app to open it.

given this code, mainly taken from the MSDN website... what should I write in place of:

//how to launch an app to read the file?

The file exists and I can get its reference by var file = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri);

but even Windows.System.Launcher.launchFileAsync(file) is not working... :( Any help?

Thanks!!!

 function create() {

 {
        localFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
           .then(function (sampleFile) {
               var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
               var timestamp = formatter.format(new Date());

               return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
           }).done(function () {



           });
    }




        }


function open() {



    localFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {

          if (sampleFile) { console.log("it does exists") }

          return Windows.Storage.FileIO.readTextAsync(sampleFile);
      }).done(function (timestamp) {
          var uri = new Windows.Foundation.Uri('ms-appx:///dataFile.txt');
          var file = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri);
          //how to launch an app to read the file?
      }, function () {
          console.log("timestamp non trovato");
      });

}

Solution

  • You have a couple of things going on here:

    1. You're using the URI scheme for a file that's part of your application, not one that's used for the local folder. Use this instead

      var uri = new Windows.Foundation.Uri('ms-appdata:///local/dataFile.txt');
      
    2. You need to use the callback from getFileFromApplicationUriAsync, try:

      localFolder.getFileAsync("dataFile.txt")
        .then(function (sampleFile) {
      
            if (sampleFile) { console.log("it does exists") }
      
            return Windows.Storage.FileIO.readTextAsync(sampleFile);
        }).done(function (timestamp) {
      
            var uri = new Windows.Foundation.Uri('ms-appdata:///local/dataFile.txt');
            Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).done(function(file)
            {
                Windows.System.Launcher.launchFileAsync(file);
      
    3. You don't actually need to create the Uri here, you could do the launch in the first 'then' (but I'm unsure of your intent since you do read the file there too). Note too, that your check for the file existing isn't quite right. If the file doesn't exist you'll trigger the error handler callback, which I've added below to output to the console

      localFolder.getFileAsync("dataFile.txt")
        .then(function (sampleFile) {
                 Windows.System.Launcher.launchFileAsync(sampleFile);
                 return Windows.Storage.FileIO.readTextAsync(sampleFile);
             },
             function () { console.log("it does not exist"); })
        .done(function (timestamp) {
      
            // not sure what you want to do here
      
        }, function () {
            console.log("timestamp non trovato");
        }
      );});