0

I have a c# script that at some point create a string variable, how can I save it as HTML file in the same directory of the script?

 string saveHtml = "<html>" +
    "<head>" +
    "  <title>Select Image</title>" +
    "  <script type=\"text/javascript\">" +
    "  function displayImage(elem) {" +
    "    var image = document.getElementById(\"canvas\");" +
    "    image.src = elem.value;        " +
    "  }" +
    "  </script>" +
    "</head>" +
    "<body>" +
    "  <form name=\"controls\">" +
    "    <img id=\"canvas\" src=\"pictures/fire1.jpg\" />" +
    "    <select name=\"imageList\" onchange=\"displayImage(this);\">" +
    "      <option value=\"pictures/001JRPchargeable.png\">Fire 1</option>" +
    "      <option value=\"pictures/001JRPNonchargeable.png\">Fire 2</option>" +
"</select>" +
"  </form>" +
"</body>" +
"</html>";
1

2 Answers 2

2

As Michael Randall mentioned:

class Program
{
    static void Main(string[] args)
    {
        string saveHtml = "<html>" +
                          "<head>" +
                          "  <title>Select Image</title>" +
                          "  <script type=\"text/javascript\">" +
                          "  function displayImage(elem) {" +
                          "    var image = document.getElementById(\"canvas\");" +
                          "    image.src = elem.value;        " +
                          "  }" +
                          "  </script>" +
                          "</head>" +
                          "<body>" +
                          "  <form name=\"controls\">" +
                          "    <img id=\"canvas\" src=\"pictures/fire1.jpg\" />" +
                          "    <select name=\"imageList\" onchange=\"displayImage(this);\">" +
                          "      <option value=\"pictures/001JRPchargeable.png\">Fire 1</option>" +
                          "      <option value=\"pictures/001JRPNonchargeable.png\">Fire 2</option>" +
                          "</select>" +
                          "  </form>" +
                          "</body>" +
                          "</html>";

        File.WriteAllText(@"C:\temp\theFile.html", saveHtml);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this code:

string saveHtml = "<html>" +
    "<head>" +
    "  <title>Select Image</title>" +
    "  <script type=\"text/javascript\">" +
    "  function displayImage(elem) {" +
    "    var image = document.getElementById(\"canvas\");" +
    "    image.src = elem.value;        " +
    "  }" +
    "  </script>" +
    "</head>" +
    "<body>" +
    "  <form name=\"controls\">" +
    "    <img id=\"canvas\" src=\"pictures/fire1.jpg\" />" +
    "    <select name=\"imageList\" onchange=\"displayImage(this);\">" +
    "      <option value=\"pictures/001JRPchargeable.png\">Fire 1</option>" +
    "      <option value=\"pictures/001JRPNonchargeable.png\">Fire 2</option>" +
"</select>" +
"  </form>" +
"</body>" +
"</html>";


string path = @"D:\Test.html";
System.IO.File.WriteAllText(path, saveHtml)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.