1

我正在尝试在文档库中创建一个新文件夹并使用 FileOpenPicker 选择一个图像来保存创建文件夹...

我面临的错误是:

mscorlib.dll 中出现“System.Exception”类型的异常,但未在用户代码中处理

附加信息:当该文件已存在时无法创建该文件。(来自 HRESULT 的异常:0x800700B7)

如果有这个异常的处理程序,程序可以安全地继续。

这是我的代码:

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    StorageFolder storage = KnownFolders.DocumentsLibrary;
    storage = await storage.CreateFolderAsync("New folder", CreationCollisionOption.GenerateUniqueName);
        
    var openpicker = new FileOpenPicker();
    openpicker.CommitButtonText = "Upload";
    openpicker.FileTypeFilter.Add(".jpg");
    openpicker.FileTypeFilter.Add(".jpeg");
    openpicker.FileTypeFilter.Add(".png");
    openpicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    openpicker.ViewMode = PickerViewMode.List;

    file = await openpicker.PickSingleFileAsync();

    var imageFile = await StorageFile.GetFileFromPathAsync(file.Path);
    IRandomAccessStream rStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
    Stream stream = rStream.AsStreamForWrite();

    if (storage != null)
    {
        if (file != null)
        {
            await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
        }
    }
}
4

1 回答 1

1

您的代码似乎不正确,或者它没有按照您的描述进行。试试这个:

var destinationFolder = await KnownFolders.DocumentsLibrary.CreateFolderAsync("New Folder", CreationCollisionOption.GenerateUniqueName);

var openpicker = new FileOpenPicker();
openpicker.CommitButtonText = "Upload";
openpicker.FileTypeFilter.Add(".jpg");
openpicker.FileTypeFilter.Add(".jpeg");
openpicker.FileTypeFilter.Add(".png");
openpicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openpicker.ViewMode = PickerViewMode.List;

file = await openpicker.PickSingleFileAsync();


if (destinationFolder != null && file !=null)
{
    await file.CopyAsync(destinationFolder);

}

对于最后一个复制文件步骤,您可能希望使用为您解决文件名冲突的重载

await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.GenerateUniqueName);
于 2012-10-04T08:34:42.377 回答