locked
Problem reading text file from OneDrive using C# in Visual Studio Express 2012 for Windows.

    Question

  •  In Visual Studio Express 2012 for Windows Phone the following C# code works fine:

    var downloafResult = await client.DownloadAsync(fileId);

    var reader = new StreamReader(downloadResult.Stream);

    text = await reader.ReadToEndAsync();

    In Visual Studio Express 2012 for Windows the following code gives a VS2012 error:

    LiveDownloadOperation op = await connectClient.CreateBackgroundDownloadAsync(fileId);
    var dlResult = await op.StartAsync();

    var reader = new StreamReader(dlResult.Stream);
    string text = await reader.ReadToEndAsync();

    Argument error - cannot convert from 'Windows.Storage.Streams.InputStream' to 'System.IO.Stream'


    I have been unable to find any information on handling the result of the LiveBackgroundDownmloadAsync function. Any help will bee greatly appreciated.

    Thanks !!!











    Saturday, October 11, 2014 7:38 PM

Answers

  • You may want to add a call to WindowsRuntimeStreamExtensions.AsStreamForRead. Here's some sample code to read an entire file from OneDrive:

            // Don't forget '/content' at the end.
            LiveDownloadOperationResult ldor = await client.BackgroundDownloadAsync(this.selectedFile.Id + @"/content");
            // Store Stream to Managed Stream.
            var stream = ldor.Stream.AsStreamForRead(0);
            StreamReader reader = new StreamReader(stream);
            // Stream to UTF-8 string.
            this.SelectedText = reader.ReadToEnd();
    The full code of a sample project can be found here.
    Monday, October 13, 2014 9:50 AM

All replies

  • You may want to add a call to WindowsRuntimeStreamExtensions.AsStreamForRead. Here's some sample code to read an entire file from OneDrive:

            // Don't forget '/content' at the end.
            LiveDownloadOperationResult ldor = await client.BackgroundDownloadAsync(this.selectedFile.Id + @"/content");
            // Store Stream to Managed Stream.
            var stream = ldor.Stream.AsStreamForRead(0);
            StreamReader reader = new StreamReader(stream);
            // Stream to UTF-8 string.
            this.SelectedText = reader.ReadToEnd();
    The full code of a sample project can be found here.
    Monday, October 13, 2014 9:50 AM
  • Diederik Krols,

    Thanks for your suggestion. It worked like a champ!

    Also, thanks for the link to your sample project on your blog. There are interesting aspects to that sample that I'm anxious to investigate also.

    Greatly appreciated

    Ed Moyle

    Monday, October 13, 2014 6:45 PM