将字符串作为文本文件上传到SkyDrive? [英] Uploading string as text file to SkyDrive?

查看:80
本文介绍了将字符串作为文本文件上传到SkyDrive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将C#与Live Connect API结合使用,以将空白(或说测试"的)文本文件上传到SkyDrive.到目前为止,我拥有的代码:

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);

其中LiveSignin()是处理登录代码并返回LiveConnectClient的函数,而getFolder(LiveConnectClient客户端)是获取要尝试上传到的文件夹ID的函数.

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.

该代码引发有关必须为"Windows.Storage.Streams.IInputStream"的空白字符串(最后一行的第三个参数)的错误,但是我似乎找不到任何有关如何转换字符串的文档到一个IInputStream,或者就此而言,我可以找到很多有关"IInputStream"的文档.

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.

在Windows Runtime/Live Connect的早期版本中(在另一个项目上),我曾经使用过:

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);

但这会引发很多错误(大多数是因为UploadAsync已被BackgroundUploadAsync取代).

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

那么,有没有一种方法可以将字符串转换为IInputStream,或者我什至不需要使用IInputStream?如果我的方法不起作用,如何将空白文本文件从C#Metro应用程序上载到SkyDrive? (如果有很大的不同,则可以在Visual Studio 2012 Express中根据Windows 8 Enterprise的评估进行开发)

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)

我终于找到了"Stream.AsInputStream",但是现在我得到了与

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

类型为'System.AccessViolationException'的未处理异常 发生在Windows.Foundation.winmd

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

现在的代码:

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);

推荐答案

今天有同样的问题,据我所知,解决此问题的唯一方法是先将您的文本写入本地文件,然后再上传.

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.

我的解决方案如下:

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);

这篇关于将字符串作为文本文件上传到SkyDrive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆