Uploading string as text file to SkyDrive?

2019-07-23 13:42发布

问题:

I'm trying to use C# with the Live Connect API to upload a blank (or one that says "test") text file to SkyDrive. The code I have so far:

LiveConnectClient client = await LiveSignin();
string folderID = await getFolder(client);
client.BackgroundUploadAsync(folderID, "pins.txt", "", OverwriteOption.Rename);

where LiveSignin() is a function that handles the sign in code and returns a LiveConnectClient, and getFolder(LiveConnectClient client) is a function that gets the folder ID that I'm trying to upload to.

That code throws an error about the blank string (third parameter on the last line) having to be a "Windows.Storage.Streams.IInputStream", but I can't seem to find any documentation on how to convert a String to an IInputStream, or, for that matter, much of any documentation on "IInputStream" that I can find.

With earlier versions of the Windows Runtime/Live Connect (on another project) I had used:

byte[] byteArray = System.Text.Encoding.Unicode.GetBytes(Doc);
MemoryStream stream = new MemoryStream(byteArray);
App.client.UploadCompleted += client_UploadCompleted;
App.client.UploadAsync(roamingSettings.Values["folderID"].ToString(), docTitle.Text + ".txt", stream);

but that throws a lot of errors now (most of them because UploadAsync has been replaced with BackgroundUploadAsync).

So, is there a way to convert a string to an IInputStream, or do I not even need to use an IInputStream? If my method just doesn't work, how would one upload a blank text file to SkyDrive from a C# Metro app? (developing in Visual Studio 2012 Express on the evaluation of Windows 8 Enterprise, if that makes much of a difference)

EDIT: I finally found "Stream.AsInputStream", but now I'm getting the same error as this

An unhandled exception of type 'System.AccessViolationException' occurred in Windows.Foundation.winmd

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt

the code now:

LiveConnectClient client = await LiveSignin();
string folderID = await getFolder(client);
Stream OrigStream = new System.IO.MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes("test"));
LiveOperationResult result = await client.BackgroundUploadAsync(folderID, "pins.txt", OrigStream.AsInputStream(), OverwriteOption.Rename);

回答1:

Hi

Had same problem today and as far as I can see the only solution to this problem is to write your text into a local file first and then upload it.

My solution looks like this:

var tmpFile= await ApplicationData.Current.
                       LocalFolder.CreateFileAsync
                         ("tmp.txt", CreationCollisionOption.ReplaceExisting);

using (var writer = new StreamWriter(await tmpFile.OpenStreamForWriteAsync()))
{
    await writer.WriteAsync("File content");
}
var operationResult = 
      await client.BackgroundUploadAsync(folderId, tmpFile.Name, tmpFile,
                                          OverwriteOption.Overwrite);


标签: c# onedrive