Search code examples
c#livewindows-phone-8onedrive

Download and launch default app in windows phone 8 in C#


I am using Live api to download SkyDrive files.

My code has a download click event which triggers the OnDownloadedCompleted function.

OnDownloadedCompleted function copies the file to "filename".

and calls the DefaultLaunch(), which takes in the "filename" and tries to launch it by the default program in windows phone 8.

When i execute this code (The file downloaded is a OneNote file) OneNote opens and says that the file can't be open.

Can anyone please help me validate this code?

Thanks a lot!

private void btnDownload_Click(object sender, RoutedEventArgs e)
    {

        if (App.Current.LiveSession == null)
        {
            infoTextBlock.Text = "You must sign in first.";
        }
        else
        {
            LiveConnectClient client = new LiveConnectClient(App.Current.LiveSession);
            client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(OnDownloadCompleted);

            client.DownloadAsync("file_id");

        }
    }

The code for OnDownloadCompleted is

void OnDownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
    {
        if (e.Result != null)
        {
            var filestream = File.Create(@"filename");
            e.Result.Seek(0, SeekOrigin.Begin);
            e.Result.CopyTo(filestream);
            filestream.Close();
            DefaultLaunch();

            e.Result.Close();

        }
        else
        {
            infoTextBlock.Text = "Error downloading image: " + e.Error.ToString();
        }
    }

The code for Default launch function is

 async void DefaultLaunch()
    {

        try
        {
            string imageFile = @"File.one";
            var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
            var success = await Windows.System.Launcher.LaunchFileAsync(file);
            if (success)
            {}
            else
            {}
        }
        catch (Exception e)
        {
            Console.WriteLine(e.StackTrace);
            Console.WriteLine(e.ToString());

        }
    }

Solution

  • try this tutorial.. http://msdn.microsoft.com/en-us/live/ff519582.aspx.. it is given there how to use live sdk in windows 8 platform