我正在开发一个应用程序,您应该可以打开并保存文件,类似于“记事本”。它是一个通用应用程序,应该适用于计算机和移动设备。
该应用程序在计算机上正常运行。如果我在本地或在Dropbox上保存或打开文件,它也可以在移动设备上正常工作。
但是如果我在OneDrive上打开一个文件(通过文件选择器),然后尝试保存一个异常被抛出:访问被拒绝。 (HRESULT异常:0x80070005(E_ACCESSDENIED))
如果我在OneDrive上选择“另存为”则可以。但每次我选择Save后,都会创建一个新的文件实例(我得到“文件1”,“文件2”,“文件3”等等。)
我只在手机上测试了这个。我的应用程序可能有一个错误,但由于它适用于DropBox,我不这么认为。这是OneDrive的已知问题吗?或者我可能只是有一些本地问题?我使用的是Windows 10 Mobile 10.10586.420和OneDrive 17.11。
以下是一些用于说明问题的代码:
<Page
x:Class="BasicEditor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BasicEditor"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="40">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="Open"
Click="OpenButton_Click" />
<Button Content="Save"
Click="SaveButton_Click" />
<Button Content="Save as"
Click="SaveAsButton_Click" />
</StackPanel>
<TextBox Grid.Row="1" x:Name="TextEditor"
TextWrapping="Wrap" />
</Grid>
</Page>
using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace BasicEditor
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
string MruToken;
private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add(".txt");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
try
{
string fileContent = await FileIO.ReadTextAsync(file);
TextEditor.Text = fileContent;
string token = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "");
MruToken = token;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write($"Open-Error: {ex.Message}");
var dialog = new MessageDialog($"Open-Error: {ex.Message}");
await dialog.ShowAsync();
}
}
}
private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
var file = await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(MruToken);
string fileContent = await FileIO.ReadTextAsync(file);
await FileIO.WriteTextAsync(file, TextEditor.Text);
var dialog = new MessageDialog($"Saved successfully");
await dialog.ShowAsync();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write($"Save-Error: {ex.Message}");
var dialog = new MessageDialog($"Save-Error: {ex.Message}");
await dialog.ShowAsync();
}
}
private async void SaveAsButton_Click(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
savePicker.SuggestedFileName = "New Document";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
try
{
await FileIO.WriteTextAsync(file, TextEditor.Text);
string token = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "");
MruToken = token;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write($"SaveAs-Error: {ex.Message}");
var dialog = new MessageDialog($"SaveAs-Error: {ex.Message}");
await dialog.ShowAsync();
}
}
}
}
}
我的应用的测试版可在此处找到:https://www.microsoft.com/store/apps/9NBLGGH3MFM3
使用OneDrive时具有完全行为的另一个应用是NotepadX。