我正在开发Windows运行时通用应用程序。
我需要将我在我的应用程序中使用的数据库上传到用户的OneDrive?
但发生FileNotFound
异常。但我知道道路是正确的。
因为我引用了sqlite文件,所以它显示了异常。如果我引用txt文件,则上传过程顺利进行。
var authClient = new LiveAuthClient();
var authResult = await authClient.LoginAsync(new string[] { "wl.skydrive", "wl.skydrive_update" });
if (authResult.Session != null)
{
var liveConnectClient = new LiveConnectClient(authResult.Session);
var FileToUpload = await ApplicationData.Current.LocalFolder.GetFileAsync("text.sqlite");//exception occurs here
var FileToUpload = await ApplicationData.Current.LocalFolder.GetFileAsync("text.txt");//no exception for txt files
var folderData = new Dictionary<string, object>();
folderData.Add("name", "Folder")
LiveOperationResult operationResult = await liveConnectClient.PostAsync("me/skydrive", folderData);
LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync(folderId, "filename", FileToUpload, OverwriteOption.Overwrite);
LiveOperationResult uploadResult = await uploadOperation.StartAsync();
HandleUploadResult(uploadResult);
}
答案 0 :(得分:1)
首先,您应该使用后台传输任务来移动较大的文件,并在用户在传输过程中接听电话时处理该场合。后台任务是关键。
有点像这样。
BackgroundUploader uploader = new BackgroundUploader();
UploadOperation upload = uploader.CreateUpload(uri, file);
await HandleUploadAsync(upload, true);
但在你做之前,你需要假设用户将操纵数据库。因此,您应该在开始上载之前复制数据库文件。代码:
var sourceFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var sourceFile = await sourceFolder.CreateFileAsync("database",
Windows.Storage.CreationCollisionOption.OpenIfExists);
var targetFolder = await sourceFolder.CreateFolderAsync("~",
Windows.Storage.CreationCollisionOption.OpenIfExists);
var targetFile = await targetFolder.CreateFileAsync(sourceFile.Name,
Windows.Storage.CreationCollisionOption.ReplaceExisting);
await sourceFile.MoveAndReplaceAsync(targetFile);
这不仅是一个好主意,我觉得它会纠正你遇到的问题。请记住,您始终可以查询并询问是否已完成转移(您是否正在进行转移)。
祝你好运!