Search code examples
c#windowsiowindows-store-apps

Windows Universal C#: Getting project files returns a FNFE


I am coding a WUP app and I need to copy a database file over to the users local folder from the application if two values are the same. I have followed many suggestions on how to access project files but none of them work and finally took the advice from Windows Developer but that is just throwing a FileNotFoundException...

My code is

try { await (await FileUtils.GetLocalFolder().GetFolderAsync("Database")).GetFileAsync("DB.json"); }
catch (FileNotFoundException d) { await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Database/DB.json"))).CopyAsync(await FileUtils.GetLocalFolder().GetFolderAsync("Database")); }

I have looked at my project directory names and file names but yet still no luck, everything looks perfectly fine.

If you need any more information on how to help fix this just ask :)


Solution

  • The following code gets your file in your project's folder:

    var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Database");
    var file = await folder.GetFileAsync("DB.json");
    

    Just make sure you have set the Build Action of db.json to "Content". See Screenshot. enter image description here

    I just checked it, it works on my machine.

    Find a the code in a working sample here: https://github.com/DanielMeixner/so/tree/master/FileAccess/App1