сохранить строку c# как файл html - PullRequest
0 голосов
/ 12 марта 2020

У меня есть сценарий c#, который в какой-то момент создает строковую переменную, как я могу сохранить его как файл HTML в том же каталоге сценария?

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

Ответы [ 2 ]

2 голосов
/ 12 марта 2020

Как Майкл Рэндалл упомянул:

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);
    }
}
1 голос
/ 12 марта 2020

Вы можете использовать этот код:

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