How do I get the contents of a file in a project?

Posts: 38
Joined: Sat Nov 19, 2016 7:23 pm


Postby tomny » Sun Jan 15, 2017 2:20 am

Dear JS-Support,

As shown, I have the name of the project file1.txt file, I want to get it in the code inside the content, how do?

Image160.png (5.24 KiB) Viewed 1500 times

Thank you.

JS-Support @Userware
Site Admin
Posts: 1067
Joined: Tue Apr 08, 2014 3:42 pm

Re: How do I get the contents of a file in a project?

Postby JS-Support @Userware » Wed Jan 18, 2017 7:22 am


The process will be easier in a future update.

With the current Beta version, here is how to do it:

1. Launch Notepad with Administrator rights

2. Use it to open the following file:
C:\Program Files (x86)\MSBuild\CSharpXamlForHtml5\InternalStuff\Targets\CSharpXamlForHtml5.Build.targets

3. Find the following line:


If it is not already there, add the ".txt" extension to the list. In other words, replace the above line with the following:


4. Save the file, and relaunch Visual Studio if it was already running.

5. Ensure that the "Build Action" of your TXT file is set as "Resource". You can do so by selecting your TXT file in the Solution Explorer and pressing the F4 key to display its properties.

The steps described above will result in the TXT file being copied to the output folder, or more precisely to the following folder: "bin\Debug\Output\Resources\YourAssemblyName\FoldersIfAny\"

Then you need to read the file from that location. To do so:

1. Create a new class named ResourcesHelper.cs, and copy/paste the following code:

Code: Select all

public static class ResourcesHelper
    public static void GetResource(string filePath, System.Action<string> callback)
        if (!CSHTML5.Interop.IsRunningInTheSimulator)
if (window.location.protocol.startsWith(""file"")) {
    alert(""To use the GetResource method, you must be running on http:// or https:// - To do so, from the CSHTML5 Simulator, click the green button '...' near 'Run in the browser, and click 'Run from localhost'."");
else {
    var rawFile = new XMLHttpRequest();""GET"", $0, false);
    rawFile.onreadystatechange = function() {
        if (rawFile.readyState === 4) {
                var allText = rawFile.responseText;
}", filePath, (System.Action<string>)callback);

2. Call the method "ResourcesHelper.GetResource()" where you need to retrieve the TXT file. Be sure to specify the location of the file relative to the Output folder. Please note that it currently won't work inside the Simulator, so you need to test in the browser (to do so, from the Simulator, click the green button labelled "..." that is near the button "Run in the browser", and click "Run from localhost").

Here is an example of code to read the file:

Code: Select all

    fileContentAsString =>

If you get any errors while running in the browser, please be sure to open the browser Console window to see the error details. To do so, under Chrome for example, press F12 to open the Developer Tools, and click the Console tab.

Please feel free to let me know if this solution works for you.


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 2 guests