{"id":281,"date":"2016-12-17T10:44:34","date_gmt":"2016-12-17T10:44:34","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=281"},"modified":"2022-09-13T15:37:18","modified_gmt":"2022-09-13T14:37:18","slug":"working-with-files-2","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/files\/working-with-files-2","title":{"rendered":"Working With Files-LocalFolder"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Filepath for object that just want a string <\/h4>\n\n\n\n<p>use the URI: ms-appdata:\/\/\/local\/SomeFolder\\SomeFile.png <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>img.Source = new BitmapImage(new Uri(\"ms-appdata:\/\/\/local\/SomeFolder\/Logo.png\"));<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstring FilePath = ApplicationData.Current.LocalFolder.Path + \"\\\\MyFolderName\\\\MyFile1.png\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Does File Exist <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstring FilePath = ApplicationData.Current.LocalFolder.Path;\n\tFilePath += \"\\\\MyFolderName\\\\\";\n\tFilePath += \"MyFile1.png\";\n\tif (File.Exists(FilePath))\n\t{\n\t}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using await<\/h5>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\"><em>(note this is not good in loops etc as await doesn&#8217;t truly await!!)<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tStorageFolder OurLocalFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(\"MyFolder\");\n\tif (await OurLocalFolder.TryGetItemAsync(\"SomeFile.png\") == null)\n\t{<\/code><\/pre>\n\n\n\n<p>or <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tStorageFolder Folder1 = ApplicationData.Current.LocalFolder;\n\tif (await Folder1.TryGetItemAsync(\"MyFolder\\\\MyFile.txt\") != null)\t\/\/(Must use \\\\ not \/)\n\t{\n\t\t\/\/File Exists<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Write Data To A Local File <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using Windows.Storage;\n\n\tStorageFolder Folder1 = ApplicationData.Current.LocalFolder;\n\tStorageFile File1 = await Folder1.CreateFileAsync(\"SomeFolder\\\\SomeFile.txt\", CreationCollisionOption.ReplaceExisting);\n\tif (File1 != null)\n\t{\n\t\tawait FileIO.WriteTextAsync(File1, \"data\");\n\t}<\/code><\/pre>\n\n\n\n<p>File will be saved to: User Files\\LocalAppData\\PackageName\\LocalState\\SomeFolder\\SomeFile.txt <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Add String Array&nbsp;To A Local File <\/h4>\n\n\n\n<p>Each string is added as a new line. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/CREATE NEW FILE OR ADD TO EXISTING FILE\n\tStorageFolder Folder1 = ApplicationData.Current.LocalFolder;\n\tStorageFile File1 = await Folder1.CreateFileAsync(\"SomeFolder\\SomeFile.txt\", CreationCollisionOption.OpenIfExists);\n\tif (File1 != null)\n\t{\n\t\tstring&#91;] TagIds = new string&#91;1];\n\t\tTagIds&#91;0] = \"hello\";\n\t\tawait FileIO.AppendLinesAsync(File1, TagIds);\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete File <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\ttry\n\t{\n\t\tStorageFolder Folder1 = ApplicationData.Current.LocalFolder;\n\t\tStorageFile File1 = await Folder1.GetFileAsync(\"MyFolder\\\\MyFile.txt\");\n\t\tif (File1 != null)\n\t\t\tawait File1.DeleteAsync(StorageDeleteOption.Default);\n\t\t\t\t\t\t\t\t\t\t\n\t\tSystem.Diagnostics.Debug.WriteLine(\"Tags file deleted\");\n\t}\n\tcatch (IOException)\n\t{\n\t\tSystem.Diagnostics.Debug.WriteLine(\"CAN'T DELETE FILE\");\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete All Files In A Directory<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>if (await ApplicationData.Current.LocalFolder.TryGetItemAsync(\"MyLogFilesDirectory\") != null)\n{\n    StorageFolder OurLocalFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(\"MyLogFilesDirectory\");       \/\/Will generate an exception if not found\n    var Files = await OurLocalFolder.GetFilesAsync();\n\n    foreach(var File in Files)\n    {\n        try\n        {\n            await File.DeleteAsync(StorageDeleteOption.Default);\n        }\n        catch (IOException)\n        {\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Copy File To Memory Stream <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tconst string MyFilePath = \"MyFile.xml\";\n\tMemoryStream MyFileMemoryStream = null;\n\n\tStorageFolder OurLocalFolder = ApplicationData.Current.LocalFolder;\n\tif (await OurLocalFolder.TryGetItemAsync(MyFilePath) != null)\n\t{\n\t\t\/\/----- FILE EXISTS -----\n\t\tvar FileToOpen = await OurLocalFolder.GetFileAsync(MyFilePath);\n\n\t\tusing (var fileStream = await FileToOpen.OpenStreamForReadAsync())\n\t\t{\n\t\t\tMyFileMemoryStream = new MemoryStream();\n\t\t\tawait fileStream.CopyToAsync(MyFileMemoryStream);\n\n\t\t\tMyFileMemoryStream.Seek(0, SeekOrigin.Begin);\n\t\t}\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Path To The Local Folder <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tStorageFolder Folder1 = ApplicationData.Current.LocalFolder;\n\tstring ThePath = Folder1.Path;<\/code><\/pre>\n\n\n\n<p>Gives: &#8220;C:\\Data\\Users\\DefaultAccount\\AppData\\Local\\Packages\\MY_APP_SYSTEM_NAME\\LocalState&#8221; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Filepath for object that just want a string use the URI: ms-appdata:\/\/\/local\/SomeFolder\\SomeFile.png Does File Exist Using await (note this is not good in loops etc as await doesn&#8217;t truly await!!) or Write Data To A Local File File will be saved to: User Files\\LocalAppData\\PackageName\\LocalState\\SomeFolder\\SomeFile.txt Add String Array&nbsp;To A Local File Each string is added as [&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-281","post","type-post","status-publish","format-standard","hentry","category-files"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/281","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=281"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":1179,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/281\/revisions\/1179"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}