{"id":399,"date":"2017-01-12T16:06:11","date_gmt":"2017-01-12T16:06:11","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=399"},"modified":"2022-09-13T15:37:32","modified_gmt":"2022-09-13T14:37:32","slug":"working-with-directories-local-folder","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/files\/working-with-directories-local-folder","title":{"rendered":"Working With Directories-Local Folder"},"content":{"rendered":"<p>&nbsp;<\/p>\n<h4>\n\tCreate Folder If It Doesn&#8217;t Exist<\/h4>\n<pre><code>\n\t\/\/ENSURE FOLDER EXISTS\n\tif (await ApplicationData.Current.LocalFolder.TryGetItemAsync(\"MySubFolder\") == null)\n\t\tawait ApplicationData.Current.LocalFolder.CreateFolderAsync(\"MySubFolder\");\n<\/code><\/pre>\n<h4>\n\tDelete All Files In A Folder<\/h4>\n<pre><code>\n\t\/\/DELETE ALL THE FILES\n\tStorageFolder Folder1 = await ApplicationData.Current.LocalFolder.GetFolderAsync(\"MySubFolder\");\n\tIReadOnlyList&lt;StorageFile&gt; FilesToBeDeleted = await Folder1.GetFilesAsync();\n\tif (FilesToBeDeleted != null)\n\t{\n\t\tforeach (StorageFile FileToDelete in FilesToBeDeleted)\n\t\t\tawait FileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete);\n\t}\n<\/code><\/pre>\n<h4>\n\tLook For Folders<\/h4>\n<pre><code>\n\tStorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();  \/\/StorageFolder object that maps all removable devices as subfolders.\n\tif (UsbDrive == null)\n\t{\n\t\tSystem.Diagnostics.Debug.WriteLine(\"USB Drive not found\");\n\t\treturn;\n\t}\n\n\t\/\/----- LOOK AT FOLDERS ON DRIVE -----\n\tIReadOnlyList&lt;StorageFolder&gt; UsbFoldersList = await UsbDrive.GetFoldersAsync();\n\tif (UsbFoldersList.Count &gt; 0)\n\t{\n\t\tforeach (StorageFolder Folder in UsbFoldersList)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"Folder found: \" + Folder.Name);\n\n\t\t}\n\t}\n<\/code><\/pre>\n<h4>\n\tCopy All Files From USB Stick To A LocalFolder<\/h4>\n<pre><code>\n\ttry\n\t{\n\t\t\/\/&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; REMEMBER WE WILL ONLY SEE FILE TYPES WE'VE DECLARED IN APPXMANIFEST!!! &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;##########~~~~~~~~&lt;&lt;&lt;&lt;&lt;&lt; LOOK!\n\n\t\tconst string LocalFolderDirectoryName = \"MyFolder\";\n\n\t\t\/\/----- TRY AND CONNECT TO USB DRIVE -----\n\t\tStorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();  \/\/StorageFolder object that maps all removable devices as subfolders.\n\t\tif (UsbDrive == null)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"CheckForNewUsbMemoryStickFiles - USB Drive not found\");\n\t\t\tCopyInUsbDriveFilesResult = \"USB drive not found - no changes made\";\n\t\t\treturn;\n\t\t}\n\n\t\t\/\/----- CHECK THERE ARE FILES ON DRIVE -----\n\t\tIReadOnlyList&lt;StorageFile&gt; FileList = await UsbDrive.GetFilesAsync();\n\t\tif (FileList.Count &lt; 1)\n\t\t{\n\t\t\tCopyInUsbDriveFilesResult = \"No valid files found on USB drive - no changes made\";\n\t\t\treturn;\n\t\t}\n\n\t\t\/\/----- DELETE THE CONTENTS OF OUR TARGET LOCAL DIRECTORY -----\n\t\t\/\/ENSURE FOLDER EXISTS\n\t\tif (await ApplicationData.Current.LocalFolder.TryGetItemAsync(LocalFolderDirectoryName) == null)\n\t\t\tawait ApplicationData.Current.LocalFolder.CreateFolderAsync(LocalFolderDirectoryName);\n\n\t\t\/\/DELETE ALL THE EXISTING FILES IN IT\n\t\tStorageFolder Folder1 = await ApplicationData.Current.LocalFolder.GetFolderAsync(LocalFolderDirectoryName);\n\t\tIReadOnlyList&lt;StorageFile&gt; FilesToBeDeleted = await Folder1.GetFilesAsync();\n\t\tif (FilesToBeDeleted != null)\n\t\t{\n\t\t\tforeach (StorageFile FileToDelete in FilesToBeDeleted)\n\t\t\t\tawait FileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete);\n\t\t}\n\n\t\t\/\/----- COPY IN ALL OF THE FILES FROM THE USB STICK -----\n\t\t\/\/StorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();\n\t\t\/\/StorageFolder Folder1 = await ApplicationData.Current.LocalFolder.GetFolderAsync(\"MySubFolder\");\n\t\tif (UsbDrive != null)\n\t\t{\n\t\t\t\/\/IReadOnlyList&lt;StorageFile&gt; FileList = await UsbDrive.GetFilesAsync();\n\t\t\tforeach (StorageFile File in FileList)\n\t\t\t\tawait File.CopyAsync(Folder1);\n\t\t}\n\n\t\t\/\/&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; REMEMBER WE WILL ONLY SEE FILE TYPES WE'VE DECLARED IN APPXMANIFEST!!! &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;##########~~~~~~~~&lt;&lt;&lt;&lt;&lt;&lt; LOOK!\n\n\t\tCopyInUsbDriveFilesResult = \"All files sucessfully copied from USB drive\";\n\t\treturn;\n\t}\n\tcatch (Exception ex)\n\t{\n\t\tSystem.Diagnostics.Debug.WriteLine(\"Exception: \" + ex.Message);\n\t\tCopyInUsbDriveFilesResult = \"An error occured: \" + ex.Message;\n\t}\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Create Folder If It Doesn&#8217;t Exist \/\/ENSURE FOLDER EXISTS if (await ApplicationData.Current.LocalFolder.TryGetItemAsync(&#8220;MySubFolder&#8221;) == null) await ApplicationData.Current.LocalFolder.CreateFolderAsync(&#8220;MySubFolder&#8221;); Delete All Files In A Folder \/\/DELETE ALL THE FILES StorageFolder Folder1 = await ApplicationData.Current.LocalFolder.GetFolderAsync(&#8220;MySubFolder&#8221;); IReadOnlyList&lt;StorageFile&gt; FilesToBeDeleted = await Folder1.GetFilesAsync(); if (FilesToBeDeleted != null) { foreach (StorageFile FileToDelete in FilesToBeDeleted) await FileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete); } Look For Folders StorageFolder UsbDrive [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-399","post","type-post","status-publish","format-standard","hentry","category-files"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":1180,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/399\/revisions\/1180"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}