在 UWP 的 LocalFolder 中创建一个文件夹,并将文件复制到其中
Create a folder in LocalFolder of UWP and copy files to it
我想动态创建文件夹,需要将文件复制到uwp app的本地文件夹。文件夹名称应该是文件名。例如,如果我上传名称为 Test01.png 的文件。然后应该创建一个名为 'Test01' 的文件夹,并且需要将 Test01.png 复制到 Test01 文件夹。如果文件已经存在,它应该显示像 "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);
}
您可以执行以下操作。我是用记事本写的,没机会测试。
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);
}
}
}
我想动态创建文件夹,需要将文件复制到uwp app的本地文件夹。文件夹名称应该是文件名。例如,如果我上传名称为 Test01.png 的文件。然后应该创建一个名为 'Test01' 的文件夹,并且需要将 Test01.png 复制到 Test01 文件夹。如果文件已经存在,它应该显示像 "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);
}
您可以执行以下操作。我是用记事本写的,没机会测试。
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);
}
}
}