Page 1 of 1

How to load the content of a project file

Posted: Wed Mar 20, 2019 5:23 am
by RaduMihai
Hi,

I would like to be able in my application to load the content of a project file
(text file, or json file containing some initial data) as in the attached picture.
I know that there was some time ago another topic "How do I get the contents of a file in a project" on the
same subject, but the solution exposed there is rather complicated and also not an elegant one.
It works, but I would prefer a more simple solution. In the answer to that topic, the moderator wrote :
"The process will be easier in a future update. With the current Beta version, here is how to do it:" but that was 2017
and I hope that in the actual version there is a better solution.

If there is such a possibility please be so kind and give me some info how to do it.

Thank you in advance.

My best regards,

RaduMihai

Re: How to load the content of a project file

Posted: Thu Mar 21, 2019 12:08 pm
by JS-Support @Userware
Hi,

Sure. You can use Application.Current.GetResourceString(fileName) where fileName is either in the format "ms-appx:///AssemblyName/FolderName/FileName" or "/AssemblyName;component/FolderName/FileName".

Please make sure that the "build action" of your file is set to "Content" in the file properties. You can verify that the file has been properly copied to the "bin\Debug\Output\app-cshtml5\res\AssemblyName\FolderName\FileName"

Thanks.
Regards
JS-Support

Re: How to load the content of a project file

Posted: Thu Mar 21, 2019 1:47 pm
by RaduMihai
Hi,

Thank you for the answer.

First I have to make some corrections to the answer :

- correctly is Application.GetResourceString(new Uri(filename));
- "Build action" has to be "Resource" not "Content"

This solution works fine for files of type: ".config", ".txt", ".xml", ".json", but how to
load other files (of any type, binary files, etc)?

I have tried Application.GetResourceStream, but I got an exception :
exception.jpg
exception.jpg (24.88 KiB) Viewed 5725 times

It is weird that the error message is about GetResourceString and not GetResourceStream.

Do you have any other idea of a possible solution to load any file?

Best regards,
RaduMihai

Re: How to load the content of a project file

Posted: Sat Mar 23, 2019 6:25 am
by RaduMihai
Hi,

For everyone who is interested about this subject, I have an idea how to load any file : convert the file
to base64, load as a text file and convert back from base64 to byte array.

Best regards,

RaduMihai