StorageFolder GetFolderFromPathAsync 抛出 Value does not fall in the expected range 错误
StorageFolder GetFolderFromPathAsync throws Value does not fall within the expected range error
我正在尝试打开一个 FileSaveDialog,当用户按下保存时,我正在 LocalFolder 中创建一个 zip 并将其移动到用户选择的位置。使用此代码,文件成功移动到所选位置,但是当我说 var storageFolder = await StorageFolder.GetFolderFromPathAsync(localfile.Path);
时出现类似 'Value does not fall within the expected range error.' 的异常请帮助。
这是完整的代码。
public async Task<bool> WriteFilesToFolder(string baseFolderName, Dictionary<string, string> contentOfFilesWithFileNames)
{
try
{
var savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("zip", new List<string>() { ".zip" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = baseFolderName;
StorageFile localfile = await savePicker.PickSaveFileAsync();
if (localfile == null)
{
return false;
}
string pathOfZip = string.Empty;
StorageFolder destinationfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(baseFolderName, CreationCollisionOption.ReplaceExisting);
if (destinationfolder != null)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", destinationfolder);
for (int i = 0; i < contentOfFilesWithFileNames.Count; i++)
{
var item = contentOfFilesWithFileNames.ElementAt(i);
string fileNameToCreate = item.Key;
string formattedFileName = fileNameToCreate.Replace(",", string.Empty).Replace("/", "-").Replace(":", "-");
var file = await destinationfolder.CreateFileAsync(string.Format("{0}.html", formattedFileName), CreationCollisionOption.ReplaceExisting);
var res = await WriteStorageFileAction(file, item.Value);
}
await Task.Run(() =>
{
string temp = Directory.GetParent(destinationfolder.Path).FullName;
pathOfZip = string.Format("{0}\{1}{2}.zip", temp, baseFolderName, DateTime.Now.ToString("yyyy-MM-dd_hh-mm"));
if (File.Exists(pathOfZip))
{
File.Delete(pathOfZip);
}
// Zipping file into the same folder throws exception that its being used by an other process.
// So zip to new location.
ZipFile.CreateFromDirectory(destinationfolder.Path, pathOfZip,
CompressionLevel.Optimal, true);
});
if (localfile != null)
{
string faToken = StorageApplicationPermissions.FutureAccessList.Add(localfile);
StorageFile file = await StorageFile.GetFileFromPathAsync(pathOfZip);
var storageFolder = await StorageFolder.GetFolderFromPathAsync(localfile.Path); //This line throws the exception.
await file.MoveAsync(storageFolder);
}
await destinationfolder.DeleteAsync();
return true;
}
}
catch (Exception ex)
{
throw;
}
return false;
}
更新:
这是 Mainifest 命名空间:
<包
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
我更新了 Package Manifest 文件,如下所示。但是我在 rescap:Capability .
下有一个绿色的涂鸦
现在报错如下:
如果你想使用StorageFolder.GetFolderFromPathAsync
方法得到一个StorageFolder对象,那么你需要传递一个文件夹路径作为参数。 localfile
对象是您从 FileSavePicker 获得的 StorageFile 对象。 localfile.Path
表示文件的位置,而不是文件夹。这就是您出现此异常的原因。
对于你的场景,如果你想得到你选择的文件的父文件夹,我建议你使用StorageFile.GetParentAsync Method
更新:
您似乎正在选取放置在 UWP 应用默认无权访问的位置的文件。所以选择器只能让你访问单个文件而你无法访问父文件夹,因为你没有访问该文件夹的权限。
请仔细阅读 File access permissions 以更好地理解此行为。
对于这样的场景,如果你想获取放置在UWP没有访问权限的某处的父文件夹。使用路径会更好。首先,您需要在清单文件中添加 broadFileSystemAccess
功能。
像这样:
<Package
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
然后,得到localfile
对象后,拆分localfile
的路径,就可以得到父文件夹的路径。
现在您可以这样调用 StorageFolder.GetFolderFromPathAsync
:
StorageFolder.GetFolderFromPathAsync(parentfolderpath)
我正在尝试打开一个 FileSaveDialog,当用户按下保存时,我正在 LocalFolder 中创建一个 zip 并将其移动到用户选择的位置。使用此代码,文件成功移动到所选位置,但是当我说 var storageFolder = await StorageFolder.GetFolderFromPathAsync(localfile.Path);
时出现类似 'Value does not fall within the expected range error.' 的异常请帮助。
这是完整的代码。
public async Task<bool> WriteFilesToFolder(string baseFolderName, Dictionary<string, string> contentOfFilesWithFileNames)
{
try
{
var savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("zip", new List<string>() { ".zip" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = baseFolderName;
StorageFile localfile = await savePicker.PickSaveFileAsync();
if (localfile == null)
{
return false;
}
string pathOfZip = string.Empty;
StorageFolder destinationfolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(baseFolderName, CreationCollisionOption.ReplaceExisting);
if (destinationfolder != null)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", destinationfolder);
for (int i = 0; i < contentOfFilesWithFileNames.Count; i++)
{
var item = contentOfFilesWithFileNames.ElementAt(i);
string fileNameToCreate = item.Key;
string formattedFileName = fileNameToCreate.Replace(",", string.Empty).Replace("/", "-").Replace(":", "-");
var file = await destinationfolder.CreateFileAsync(string.Format("{0}.html", formattedFileName), CreationCollisionOption.ReplaceExisting);
var res = await WriteStorageFileAction(file, item.Value);
}
await Task.Run(() =>
{
string temp = Directory.GetParent(destinationfolder.Path).FullName;
pathOfZip = string.Format("{0}\{1}{2}.zip", temp, baseFolderName, DateTime.Now.ToString("yyyy-MM-dd_hh-mm"));
if (File.Exists(pathOfZip))
{
File.Delete(pathOfZip);
}
// Zipping file into the same folder throws exception that its being used by an other process.
// So zip to new location.
ZipFile.CreateFromDirectory(destinationfolder.Path, pathOfZip,
CompressionLevel.Optimal, true);
});
if (localfile != null)
{
string faToken = StorageApplicationPermissions.FutureAccessList.Add(localfile);
StorageFile file = await StorageFile.GetFileFromPathAsync(pathOfZip);
var storageFolder = await StorageFolder.GetFolderFromPathAsync(localfile.Path); //This line throws the exception.
await file.MoveAsync(storageFolder);
}
await destinationfolder.DeleteAsync();
return true;
}
}
catch (Exception ex)
{
throw;
}
return false;
}
更新:
这是 Mainifest 命名空间:
<包
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
我更新了 Package Manifest 文件,如下所示。但是我在 rescap:Capability .
下有一个绿色的涂鸦现在报错如下:
如果你想使用StorageFolder.GetFolderFromPathAsync
方法得到一个StorageFolder对象,那么你需要传递一个文件夹路径作为参数。 localfile
对象是您从 FileSavePicker 获得的 StorageFile 对象。 localfile.Path
表示文件的位置,而不是文件夹。这就是您出现此异常的原因。
对于你的场景,如果你想得到你选择的文件的父文件夹,我建议你使用StorageFile.GetParentAsync Method
更新:
您似乎正在选取放置在 UWP 应用默认无权访问的位置的文件。所以选择器只能让你访问单个文件而你无法访问父文件夹,因为你没有访问该文件夹的权限。
请仔细阅读 File access permissions 以更好地理解此行为。
对于这样的场景,如果你想获取放置在UWP没有访问权限的某处的父文件夹。使用路径会更好。首先,您需要在清单文件中添加 broadFileSystemAccess
功能。
像这样:
<Package
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
然后,得到localfile
对象后,拆分localfile
的路径,就可以得到父文件夹的路径。
现在您可以这样调用 StorageFolder.GetFolderFromPathAsync
:
StorageFolder.GetFolderFromPathAsync(parentfolderpath)