在 C# 中创建文件 (.htm)

发布于 2024-12-10 21:47:44 字数 77 浏览 0 评论 0 原文

我想知道使用 C# 创建简单 html 文件的最佳方法。

它使用类似 System.IO.File.Create 的东西吗?

I would like to know the best way to create a simple html file using c#.

Is it using something like System.IO.File.Create?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一场春暖 2024-12-17 21:47:44

像这样的东西——

using (FileStream fs = new FileStream("test.htm", FileMode.Create)) 
{ 
    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) 
    { 
        w.WriteLine("<H1>Hello</H1>"); 
    } 
} 

Something like -

using (FileStream fs = new FileStream("test.htm", FileMode.Create)) 
{ 
    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) 
    { 
        w.WriteLine("<H1>Hello</H1>"); 
    } 
} 
甜点 2024-12-17 21:47:44

我会说 File.WriteAllText 是一种愚蠢的方法为 C# >= 3.5 编写一个文本文件。

File.WriteAllText("myfile.htm", @"<html><body>Hello World</body></html>");

我什至会说 File.WriteAllLines 足够愚蠢编写更大的 html,而无需过多地处理字符串组合。但“好”版本仅适用于 C# 4.0(稍差的版本是 C# >= 2.0)

List<string> lines = new List<string>();
lines.Add("<html>");
lines.Add("<body>");
lines.Add("Hello World");
lines.Add("</body>");
lines.Add("</html>");

File.WriteAllLines("myfile.htm", lines);
// With C# 3.5
File.WriteAllLines("myfile.htm", lines.ToArray());

I'll say that File.WriteAllText is a stupid-proof way to write a text file for C# >= 3.5.

File.WriteAllText("myfile.htm", @"<html><body>Hello World</body></html>");

I'll even say that File.WriteAllLines is stupid-proof enough to write bigger html without fighting too much with string composition. But the "good" version is only for C# 4.0 (a little worse version is C# >= 2.0)

List<string> lines = new List<string>();
lines.Add("<html>");
lines.Add("<body>");
lines.Add("Hello World");
lines.Add("</body>");
lines.Add("</html>");

File.WriteAllLines("myfile.htm", lines);
// With C# 3.5
File.WriteAllLines("myfile.htm", lines.ToArray());
梦里南柯 2024-12-17 21:47:44

如果创建文件时没有所有数据,我会使用 File.Create 然后打开该文件的 StreamWriter。
这是 MS 的一个示例,可能会对您有所帮助

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file.
        using (FileStream fs = File.Create(path, 1024)) 
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

        // Open the stream and read it back.
        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}

I would go with File.Create and then open a StreamWriter to that file if you dont have all the data when you create the file.
This is a example from MS that may help you

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file.
        using (FileStream fs = File.Create(path, 1024)) 
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

        // Open the stream and read it back.
        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}
你列表最软的妹 2024-12-17 21:47:44

查看 HtmlTextWriter 类。有关如何使用此类的示例,请查看 http://www.dotnetperls.com/htmltextwriter

Have a look at the HtmlTextWriter class. For an example how to use this class, for example look at http://www.dotnetperls.com/htmltextwriter.

○愚か者の日 2024-12-17 21:47:44

读写文本文件MSDN 信息。 HTML 只是一个带有 *.HTML 扩展名的简单文本文件;)

Reading and writing text files and MSDN info. HTML is just a simple text file with *.HTML extension ;)

你爱我像她 2024-12-17 21:47:44

Simply opening a file for writing (using File.OpenWrite() for example) will create the file if it does not yet exist.

梦幻的心爱 2024-12-17 21:47:44

如果您查看 http://msdn.microsoft.com/en-us /library/d62kzs03.aspx 您可以找到创建文件的示例。

但是你想如何创建 html 文件内容呢?如果这只是静态的,那么您可以将其写入文件。如果您必须动态创建 html,您可以使用具有正确标记的 ASPX 文件,并使用 Server.Execute 将 HTML 作为字符串获取。

If you have a look at http://msdn.microsoft.com/en-us/library/d62kzs03.aspx you can find an example of creating a file.

But how do you want to create the html file content? If that's just static then you can just write it to a file.. if you have to create the html on the fly you could use an ASPX file with the correct markup and use a Server.Execute to get the HTML as a string.

So要识趣 2024-12-17 21:47:44

是的,System.IO.File.Create(Path) 会很好地创建您的文件。
您还可以使用filestream并对其进行写入。看来写个htm文件更方便

Yep, System.IO.File.Create(Path) will create your file just fine.
You can also use a filestream and write to it. Seems more handy to write a htm file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文