2017-12-06 8 views
0

フォルダを動的に作成し、ファイルをuwp appのローカルフォルダにコピーする必要があります。フォルダ名はファイル名でなければなりません。たとえば、名前がTest01.pngのファイルをアップロードするとします。フォルダ名は 'Test01'で作成し、Test01.pngをTest01フォルダにコピーする必要があります。ファイルがすでに存在する場合は、「ファイルが既に存在し、置き換える必要があります」のような警告が表示されます。UWPのLocalFolderにフォルダを作成してファイルをコピーします

 FileOpenPicker openPicker = new FileOpenPicker(); 
     openPicker.ViewMode = PickerViewMode.Thumbnail; 
     openPicker.SuggestedStartLocation = PickerLocationId.Desktop; 

     foreach (string extension in FileExtensions.Video) 
     { 
      openPicker.FileTypeFilter.Add(extension); 
     } 

     file = await openPicker.PickSingleFileAsync(); 
     if (file != null) 
     { 
      StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; 
      await ApplicationData.Current.LocalFolder.CreateFolderAsync("Data");//need to change the folder name with filename 
      string desiredName = file.Name; 
      //should copy it to subfolder and raise alert if already exist 
      StorageFile newFile = await localFolder.CreateFileAsync(desiredName, CreationCollisionOption.FailIfExists); 

     } 

答えて

0

あなたができることは次のとおりです。私はこれをメモ帳に書いており、これをテストする機会はありませんでした。

FileOpenPicker openPicker = new FileOpenPicker(); 
openPicker.ViewMode = PickerViewMode.Thumbnail; 
openPicker.SuggestedStartLocation = PickerLocationId.Desktop; 

foreach (string extension in FileExtensions.Video) 
{ 
    openPicker.FileTypeFilter.Add(extension); 
} 

file = await openPicker.PickSingleFileAsync(); 
if (file != null) 
{ 
    StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; 

    string folderName = System.IO.Path.GetFileNameWithoutExtension(file.Name); //folder name with filename 

    ////await ApplicationData.Current.LocalFolder.CreateFolderAsync("Data");//need to change the folder name with filename 

    StorageFolder testFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("test", CreationCollisionOption.OpenIfExists); 

    ////StorageFolder newFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists); 

    StorageFolder newFolder = await testFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists); 

    string desiredName = file.Name; 
    //should copy it to subfolder and raise alert if already exist 

    ////StorageFile newFile = await localFolder.CreateFileAsync(desiredName, CreationCollisionOption.FailIfExists); 

    try 
    { 
     await file.CopyAsync(newFolder, desiredName, NameCollisionOption.FailIfExists); 
    } 
    catch(Exception exp) 
    { 
     //show here messagebox that is exists 
     Windows.UI.Xaml.Controls.ContentDialog replacePromptDialog = new Windows.UI.Xaml.Controls.ContentDialog() 
     { 
      Title = "File exists in the new location", 
      Content = "Do you want to replace the old file with the new file?", 
      CloseButtonText = "Keep the old one", 
      PrimaryButtonText = "Replace with new one" 
     }; 
     Windows.UI.Xaml.Controls.ContentDialogResult result = await replacePromptDialog.ShowAsync(); 
     if (result == Windows.UI.Xaml.Controls.ContentDialogResult.Primary) 
     { 
      await file.CopyAsync(newFolder, desiredName, NameCollisionOption.ReplaceExisting); 
     } 
    } 

} 
+0

ここでテストします。これに共通のルートフォルダ(「Test」という名前)を追加できますか? Localstate> Test> Test01> Test01.txtを意味します。ローカル州>テスト> Test02> Test02.txt? – nsds

+0

sfとは何ですか? sf.CopyAsync – nsds

+0

sfはあなたのファイルです。コードを更新しました。 「これに共通する」という言葉が何であるか分かりませんが、このコードではあなたが望むことができます。やってみて。 –

関連する問題