Create a folder in LocalFolder of UWP and copy files to it

1.9k views Asked by At

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

        }
1

There are 1 answers

4
Vaibhav J. On BEST ANSWER

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

}