我正在创建一个 UWP 应用程序,其中将文件存储在本地文件夹中。一切正常,直到我尝试保存 .doc 文件。
1)我正在保存带有扩展名的文件(例如:filename.mp3、filename.flv、filename.png)。
2) 保存此文件时我没有收到任何异常。
3) 但是当我尝试保存 .doc 或 .docx 文件时,例如“filename.doc”,我收到“UnauthorizedAccessException”异常。
4)这里重要的是,如果我尝试在文件名“filename”而不是“filename.doc”中保存没有扩展名的相同文件,它工作正常,没有引发异常。
5) 仅在使用扩展名 "filename.doc" 保存时出现异常,这也仅适用于 ".doc" 和 ".docx" 文件类型。
这是代码
StorageFolder fileFolder = await HelperFunctions.GetSubFolder(4);
fileName = fileName + file.FileType;
StorageFile storageFile = await fileFolder.CreateFileAsync(fileName);
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
using (IRandomAccessStream tempStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
await RandomAccessStream.CopyAndCloseAsync(fileStream.GetInputStreamAt(0), tempStream.GetOutputStreamAt(0));
}
这是 GetSubFolder 方法
public static async Task<StorageFolder> GetSubFolder(int type)
{
StorageFolder mainFolder = ApplicationData.Current.LocalFolder;
Storage
Folder subFolder;
if (type == 0)
{
var result = await mainFolder.TryGetItemAsync(Constants.TextNotesFilesFolderName);
//creating seperate folder for text files if not exist
if (result == null)
subFolder = await mainFolder.CreateFolderAsync(Constants.TextNotesFilesFolderName);
else
subFolder = await mainFolder.GetFolderAsync(Constants.TextNotesFilesFolderName);
return subFolder;
}
else if (type == 1)
{
var result = await mainFolder.TryGetItemAsync(Constants.AudioFilesFolderName);
//creating seperate folder for audio files if not exist
if (result == null)
subFolder = await mainFolder.CreateFolderAsync(Constants.AudioFilesFolderName);
else
subFolder = await mainFolder.GetFolderAsync(Constants.AudioFilesFolderName);
return subFolder;
}
else if (type == 2)
{
var result = await mainFolder.TryGetItemAsync(Constants.ImageFilesFolderName);
//creating seperate folder for image files if not exist
if (result == null)
subFolder = await mainFolder.CreateFolderAsync(Constants.ImageFilesFolderName);
else
subFolder = await mainFolder.GetFolderAsync(Constants.ImageFilesFolderName);
return subFolder;
}
else if (type == 3)
{
var result = await mainFolder.TryGetItemAsync(Constants.ChecklistFilesFolderName);
//creating seperate folder for checklist files if not exist
if (result == null)
subFolder = await mainFolder.CreateFolderAsync(Constants.ChecklistFilesFolderName);
else
subFolder = await mainFolder.GetFolderAsync(Constants.ChecklistFilesFolderName);
return subFolder;
}
else
{
var result = await mainFolder.TryGetItemAsync(Constants.FileCardsFolderName);
//creating seperate folder for text files if not exist
if (result == null)
subFolder = await mainFolder.CreateFolderAsync(Constants.FileCardsFolderName);
else
subFolder = await mainFolder.GetFolderAsync(Constants.FileCardsFolderName);
return subFolder;
}
}
谁能帮我解决这个问题。在此先感谢,Noorul。