create a folder and save the choosing image in createfolder using fileopenpicker in windows 8

1

i'm trying to create a newfolder in documentlibrary and using FileOpenPicker to choose an image to save the createfolder...

I'm facing the error is:

An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code

Additional information: Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)

If there is a handler for this exception, the program may be safely continued.

This is my code:

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);
        }
    }
}
c#
microsoft-metro
asked on Stack Overflow Oct 4, 2012 by Navanee Baskaran • edited Jun 20, 2020 by Community

1 Answer

1

Your code seems incorrect, or its not doing what you are describing. Try this instead:

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);

}

for this last copy file step, you may want to use the overload that resolves file name conflicts for you

await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.GenerateUniqueName);
answered on Stack Overflow Oct 4, 2012 by Dangling Neuron

User contributions licensed under CC BY-SA 3.0