Matteo Sala
Matteo Sala

Reputation: 55

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

Upvotes: 0

Views: 3478

Answers (2)

Daniel
Daniel

Reputation: 9849

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);
    }
}

Upvotes: 2

Fred
Fred

Reputation: 3441

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)

Upvotes: 1

Related Questions