在WinRT应用中访问mp3文件时出现问题。
当System尝试按名称在与FileOpenPicker返回的文件相同的文件夹中打开mp3文件时,会发生'System.UnauthorizedAccessException'。换句话说,用户在与mp3文件同名的Documents中选择一个info文件。应用程序打开信息文件就好了,但无法打开mp3文件。
例如:我有一对文件(file1.info)和(file1.mp3)。文件选择器允许选择(* .info)文件。
用户选择(file1.info)。然后应用程序打开(file1.info)和(file1.mp3)。这两个文件都位于DocumentsLibrary文件夹中,但不在MusicLibrary中。问题是当我尝试打开(file1.mp3)时,我得到'UnauthorizedAccessException'。
预先解决问题:
文件: 将mp3文件复制到Documents。 创建一个与mp3文件具有相同基本名称的文本文件,并将其扩展名更改为.info。
在Package.appxmanifest>声明添加“文件类型关联”声明。检查'打开是否安全'。添加
支持的文件类型'.mp3'和'.info'。将“内容类型”留空。
代码:
Dim file as StorageFile
Dim fileopenpicker As FileOpenPicker
Dim infofile As StorageFile
Dim mp3file As StorageFile
Dim filename As String
fileopenpicker = New FileOpenPicker()
fileopenpicker.FileTypeFilter.Add(".info")
fileopenpicker.FileTypeFilter.Add(".mp3")
fileopenpicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
file = Await fileopenpicker.PickSingleFileAsync()
If file.Path.EndsWith(".info") Then
infofile = file
filename = file.Path.Substring(0, file.Path.Length - 4) & "mp3"
' This command fails with 'System.UnauthorizedAccessException'
mp3file = Await StorageFile.GetFileFromPathAsync(filename)
Else 'file is an mp3 file
mp3file = file
filename = file.Path.Substring(0, file.Path.Length - 3) & "info"
' This command succeeds!
infofile = Await StorageFile.GetFileFromPathAsync(filename)
End If
因此,当fileopenpicker实际上没有选择文件时,打开mp3文件会出现一些特定问题。
答案 0 :(得分:2)
我使用具有文档库功能的应用程序检查了此问题,并声明了文件类型.mp3和.info。我发现它似乎是一个非常奇怪的错误。如果在打开FileOpenPicker后使用大写驱动器号将路径传递到文档库文件夹,则会出现UnauthorizedAccessException。使用带小写驱动器号的路径有效。奇怪的是,在打开FileOpenPicker之前,您可以使用大写的驱动器号。
所以解决方法是小写路径。
这是我使用的代码(C#):
// Trying to get some files from the documents library
// Note: F:\Program Data is my primary documents library folder
string mp3FilePath = @"F:\Program Data\2Mann1Maus.mp3";
// This works even if the drive letter is uppercase
StorageFile file1 = await StorageFile.GetFileFromPathAsync(mp3FilePath);
// It also works with a lowercase drive letter
string infoFilePath = @"f:\Program Data\2Mann1Maus.info";
StorageFile file2 = await StorageFile.GetFileFromPathAsync(infoFilePath);
FileOpenPicker fileopenpicker = new FileOpenPicker();
fileopenpicker.FileTypeFilter.Add(".info");
fileopenpicker.FileTypeFilter.Add(".mp3");
fileopenpicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
StorageFile file = await fileopenpicker.PickSingleFileAsync();
if (file.Path.EndsWith(".info"))
{
string filename = file.Path.Substring(0, file.Path.Length - 4) + "mp3";
// This works
string testFileName1 = filename.Substring(0, 1).ToLower() +
filename.Substring(1, filename.Length - 1);
StorageFile mp3file1 = await StorageFile.GetFileFromPathAsync(testFileName1);
// This works as well
string testFileName2 = filename.ToLower();
StorageFile mp3file2 = await StorageFile.GetFileFromPathAsync(testFileName2);
// This does cause an UnauthorizedAccessException
StorageFile mp3file3 = await StorageFile.GetFileFromPathAsync(filename);
}
else
{
StorageFile mp3file = file;
String filename = file.Path.Substring(0, file.Path.Length - 3) + "info";
// This works
string testFileName1 = filename.Substring(0, 1).ToLower() +
filename.Substring(1, filename.Length - 1);
StorageFile infoFile1 = await StorageFile.GetFileFromPathAsync(testFileName1);
// This works as well
string testFileName2 = filename.ToLower();
StorageFile infoFile2 = await StorageFile.GetFileFromPathAsync(testFileName2);
// This does cause an UnauthorizedAccessException
StorageFile infoFile3 = await StorageFile.GetFileFromPathAsync(filename);
}