Create Folder If It Doesn’t Exist
//ENSURE FOLDER EXISTS
if (await ApplicationData.Current.LocalFolder.TryGetItemAsync("MySubFolder") == null)
await ApplicationData.Current.LocalFolder.CreateFolderAsync("MySubFolder");
Delete All Files In A Folder
//DELETE ALL THE FILES
StorageFolder Folder1 = await ApplicationData.Current.LocalFolder.GetFolderAsync("MySubFolder");
IReadOnlyList<StorageFile> FilesToBeDeleted = await Folder1.GetFilesAsync();
if (FilesToBeDeleted != null)
{
foreach (StorageFile FileToDelete in FilesToBeDeleted)
await FileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
Look For Folders
StorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault(); //StorageFolder object that maps all removable devices as subfolders.
if (UsbDrive == null)
{
System.Diagnostics.Debug.WriteLine("USB Drive not found");
return;
}
//----- LOOK AT FOLDERS ON DRIVE -----
IReadOnlyList<StorageFolder> UsbFoldersList = await UsbDrive.GetFoldersAsync();
if (UsbFoldersList.Count > 0)
{
foreach (StorageFolder Folder in UsbFoldersList)
{
System.Diagnostics.Debug.WriteLine("Folder found: " + Folder.Name);
}
}
Copy All Files From USB Stick To A LocalFolder
try
{
//>>>>>>>>>>>>>>>>> REMEMBER WE WILL ONLY SEE FILE TYPES WE'VE DECLARED IN APPXMANIFEST!!! <<<<<<<<<<<<<<<<<<##########~~~~~~~~<<<<<< LOOK!
const string LocalFolderDirectoryName = "MyFolder";
//----- TRY AND CONNECT TO USB DRIVE -----
StorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault(); //StorageFolder object that maps all removable devices as subfolders.
if (UsbDrive == null)
{
System.Diagnostics.Debug.WriteLine("CheckForNewUsbMemoryStickFiles - USB Drive not found");
CopyInUsbDriveFilesResult = "USB drive not found - no changes made";
return;
}
//----- CHECK THERE ARE FILES ON DRIVE -----
IReadOnlyList<StorageFile> FileList = await UsbDrive.GetFilesAsync();
if (FileList.Count < 1)
{
CopyInUsbDriveFilesResult = "No valid files found on USB drive - no changes made";
return;
}
//----- DELETE THE CONTENTS OF OUR TARGET LOCAL DIRECTORY -----
//ENSURE FOLDER EXISTS
if (await ApplicationData.Current.LocalFolder.TryGetItemAsync(LocalFolderDirectoryName) == null)
await ApplicationData.Current.LocalFolder.CreateFolderAsync(LocalFolderDirectoryName);
//DELETE ALL THE EXISTING FILES IN IT
StorageFolder Folder1 = await ApplicationData.Current.LocalFolder.GetFolderAsync(LocalFolderDirectoryName);
IReadOnlyList<StorageFile> FilesToBeDeleted = await Folder1.GetFilesAsync();
if (FilesToBeDeleted != null)
{
foreach (StorageFile FileToDelete in FilesToBeDeleted)
await FileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
//----- COPY IN ALL OF THE FILES FROM THE USB STICK -----
//StorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();
//StorageFolder Folder1 = await ApplicationData.Current.LocalFolder.GetFolderAsync("MySubFolder");
if (UsbDrive != null)
{
//IReadOnlyList<StorageFile> FileList = await UsbDrive.GetFilesAsync();
foreach (StorageFile File in FileList)
await File.CopyAsync(Folder1);
}
//>>>>>>>>>>>>>>>>> REMEMBER WE WILL ONLY SEE FILE TYPES WE'VE DECLARED IN APPXMANIFEST!!! <<<<<<<<<<<<<<<<<<##########~~~~~~~~<<<<<< LOOK!
CopyInUsbDriveFilesResult = "All files sucessfully copied from USB drive";
return;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
CopyInUsbDriveFilesResult = "An error occured: " + ex.Message;
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.