Filepath for object that just want a string

use the URI: ms-appdata:///local/SomeFolder\SomeFile.png

img.Source = new BitmapImage(new Uri("ms-appdata:///local/SomeFolder/Logo.png"));
	string FilePath = ApplicationData.Current.LocalFolder.Path + "\\MyFolderName\\MyFile1.png";

Does File Exist

	string FilePath = ApplicationData.Current.LocalFolder.Path;
	FilePath += "\\MyFolderName\\";
	FilePath += "MyFile1.png";
	if (File.Exists(FilePath))
	{
	}
Using await

(note this is not good in loops etc as await doesn’t truly await!!)

	StorageFolder OurLocalFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("MyFolder");
	if (await OurLocalFolder.TryGetItemAsync("SomeFile.png") == null)
	{

or

	StorageFolder Folder1 = ApplicationData.Current.LocalFolder;
	if (await Folder1.TryGetItemAsync("MyFolder\\MyFile.txt") != null)	//(Must use \\ not /)
	{
		//File Exists

Write Data To A Local File

using Windows.Storage;

	StorageFolder Folder1 = ApplicationData.Current.LocalFolder;
	StorageFile File1 = await Folder1.CreateFileAsync("SomeFolder\\SomeFile.txt", CreationCollisionOption.ReplaceExisting);
	if (File1 != null)
	{
		await FileIO.WriteTextAsync(File1, "data");
	}

File will be saved to: User Files\LocalAppData\PackageName\LocalState\SomeFolder\SomeFile.txt

Add String Array To A Local File

Each string is added as a new line.

	//CREATE NEW FILE OR ADD TO EXISTING FILE
	StorageFolder Folder1 = ApplicationData.Current.LocalFolder;
	StorageFile File1 = await Folder1.CreateFileAsync("SomeFolder\SomeFile.txt", CreationCollisionOption.OpenIfExists);
	if (File1 != null)
	{
		string[] TagIds = new string[1];
		TagIds[0] = "hello";
		await FileIO.AppendLinesAsync(File1, TagIds);
	}

Delete File

	try
	{
		StorageFolder Folder1 = ApplicationData.Current.LocalFolder;
		StorageFile File1 = await Folder1.GetFileAsync("MyFolder\\MyFile.txt");
		if (File1 != null)
			await File1.DeleteAsync(StorageDeleteOption.Default);
										
		System.Diagnostics.Debug.WriteLine("Tags file deleted");
	}
	catch (IOException)
	{
		System.Diagnostics.Debug.WriteLine("CAN'T DELETE FILE");
	}

Delete All Files In A Directory

if (await ApplicationData.Current.LocalFolder.TryGetItemAsync("MyLogFilesDirectory") != null)
{
    StorageFolder OurLocalFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("MyLogFilesDirectory");       //Will generate an exception if not found
    var Files = await OurLocalFolder.GetFilesAsync();

    foreach(var File in Files)
    {
        try
        {
            await File.DeleteAsync(StorageDeleteOption.Default);
        }
        catch (IOException)
        {
        }
    }
}

Copy File To Memory Stream

	const string MyFilePath = "MyFile.xml";
	MemoryStream MyFileMemoryStream = null;

	StorageFolder OurLocalFolder = ApplicationData.Current.LocalFolder;
	if (await OurLocalFolder.TryGetItemAsync(MyFilePath) != null)
	{
		//----- FILE EXISTS -----
		var FileToOpen = await OurLocalFolder.GetFileAsync(MyFilePath);

		using (var fileStream = await FileToOpen.OpenStreamForReadAsync())
		{
			MyFileMemoryStream = new MemoryStream();
			await fileStream.CopyToAsync(MyFileMemoryStream);

			MyFileMemoryStream.Seek(0, SeekOrigin.Begin);
		}
	}

Path To The Local Folder

	StorageFolder Folder1 = ApplicationData.Current.LocalFolder;
	string ThePath = Folder1.Path;

Gives: “C:\Data\Users\DefaultAccount\AppData\Local\Packages\MY_APP_SYSTEM_NAME\LocalState”

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.

Comments

Your email address will not be published. Required fields are marked *