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);
}
}
}
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);
User contributions licensed under CC BY-SA 3.0