即使启用了 broadFileSystemAccess 也无法访问文件
Problem accessing a file even with broadFileSystemAccess enabled
我有一个关联了文本和音频文件的 UWP 桌面应用程序,例如,当用户选择 Class1.txt 文件时,该应用程序会自动尝试打开 Class1.mp3 文件。
即使配置了 broadFileSystemAccess,该操作始终 returns 访问被拒绝错误。
欢迎任何帮助。谢谢
private async void nviOpen_Tapped(object sender, TappedRoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add(".txt");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
try
{
using (var txtStream = await file.OpenStreamForReadAsync())
{
var encoding = FileEncoding.DetectFileEncoding(txtStream);
txtStream.Seek(0, SeekOrigin.Begin);
var stmReader = new StreamReader(txtStream, encoding);
var txtContent = await stmReader.ReadToEndAsync();
tbxOriginalText.Text = txtContent;
}
//Open associated audio file
var audioFile = await StorageFile.GetFileFromPathAsync(file.Path + @"\" + file.DisplayName + ".mp3");
if (audioFile != null)
{
MediaPlaybackItem mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(audioFile));
}
}
catch (Exception ex)
{
MessageDialog msgDlg = new MessageDialog(ex.Message);
await msgDlg.ShowAsync();
}
}
}
请查看文件系统隐私设置,确保您已允许您的应用访问您的文件系统。
像这样:
我有一个关联了文本和音频文件的 UWP 桌面应用程序,例如,当用户选择 Class1.txt 文件时,该应用程序会自动尝试打开 Class1.mp3 文件。 即使配置了 broadFileSystemAccess,该操作始终 returns 访问被拒绝错误。 欢迎任何帮助。谢谢
private async void nviOpen_Tapped(object sender, TappedRoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add(".txt");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
try
{
using (var txtStream = await file.OpenStreamForReadAsync())
{
var encoding = FileEncoding.DetectFileEncoding(txtStream);
txtStream.Seek(0, SeekOrigin.Begin);
var stmReader = new StreamReader(txtStream, encoding);
var txtContent = await stmReader.ReadToEndAsync();
tbxOriginalText.Text = txtContent;
}
//Open associated audio file
var audioFile = await StorageFile.GetFileFromPathAsync(file.Path + @"\" + file.DisplayName + ".mp3");
if (audioFile != null)
{
MediaPlaybackItem mediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(audioFile));
}
}
catch (Exception ex)
{
MessageDialog msgDlg = new MessageDialog(ex.Message);
await msgDlg.ShowAsync();
}
}
}
请查看文件系统隐私设置,确保您已允许您的应用访问您的文件系统。
像这样: