猿问

将 ac# 字符串保存为 html 文件

我有一个 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>";


当年话下
浏览 145回答 2
2回答

慕桂英4014372

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

慕后森

您可以使用此代码:string saveHtml = "<html>" +&nbsp; &nbsp; "<head>" +&nbsp; &nbsp; "&nbsp; <title>Select Image</title>" +&nbsp; &nbsp; "&nbsp; <script type=\"text/javascript\">" +&nbsp; &nbsp; "&nbsp; function displayImage(elem) {" +&nbsp; &nbsp; "&nbsp; &nbsp; var image = document.getElementById(\"canvas\");" +&nbsp; &nbsp; "&nbsp; &nbsp; image.src = elem.value;&nbsp; &nbsp; &nbsp; &nbsp; " +&nbsp; &nbsp; "&nbsp; }" +&nbsp; &nbsp; "&nbsp; </script>" +&nbsp; &nbsp; "</head>" +&nbsp; &nbsp; "<body>" +&nbsp; &nbsp; "&nbsp; <form name=\"controls\">" +&nbsp; &nbsp; "&nbsp; &nbsp; <img id=\"canvas\" src=\"pictures/fire1.jpg\" />" +&nbsp; &nbsp; "&nbsp; &nbsp; <select name=\"imageList\" onchange=\"displayImage(this);\">" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; <option value=\"pictures/001JRPchargeable.png\">Fire 1</option>" +&nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; <option value=\"pictures/001JRPNonchargeable.png\">Fire 2</option>" +"</select>" +"&nbsp; </form>" +"</body>" +"</html>";string path = @"D:\Test.html";System.IO.File.WriteAllText(path, saveHtml)
随时随地看视频慕课网APP

相关分类

Html5
我要回答