2012-10-04 76 views
1

我想要創建一個documentlibrary和newfolder使用FileOpenPicker選擇圖像保存createfolder保存選擇圖像中createfolder ...創建一個文件夾,並使用fileopenpicker在Windows 8

我面對錯誤是:

類型的異常「System.Exception的」發生在mscorlib.dll但在用戶代碼

其他信息沒有處理:當文件已存在無法創建文件。 (來自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); 
     } 
    } 
} 

回答

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

它的過載,S現在的工作。謝謝你的回答神經元 –

相關問題