Search code examples
c#htmlstringsave-as

save a c# string as a html file


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>";

Solution

  • 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)