0

I want to create folders dynamically and need to copy files to local folder of uwp app. Folder name should be the filename. For example if I upload a file with name Test01.png. Then a folder should create with name 'Test01' and need to copy Test01.png to Test01 folder. If the file already exist it should show alert like "file already exist,need to replace".

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

        }
nsds
  • 961
  • 4
  • 13
  • 39

1 Answers1

0

Here's what you could do. I've written this in notepad and did not get a chance to test this.

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

}
Vaibhav J.
  • 352
  • 2
  • 15
  • will test here. Can I add a root folder(named 'Test') common to all this? means Localstate > Test > Test01> Test01.txt . Localstate > Test > Test02> Test02.txt ? – nsds Dec 06 '17 at 06:37
  • what is sf? in await sf.CopyAsync – nsds Dec 06 '17 at 06:44
  • sf is nothing but your file. I've updated the code. I'm not sure what you mean by "common to all this ..." but this code will do what you want. try it out. – Vaibhav J. Dec 06 '17 at 06:52
  • "common to all this ..." means there should be a root folder named "test". and files selected using picker should go to this test/test01/test01.txt – nsds Dec 06 '17 at 06:56