在 .NET 中创建文件的最快且有效的方法

发布于 2024-09-11 23:57:39 字数 543 浏览 5 评论 0原文

我有一个包含 30000 个项目的数组列表,从 ASP.NEt 页面动态创建文本文件的最佳方法是什么?目前我正在使用下面的代码,但它会因大数据而超时,

Using fileStr As New FileStream(sFileName, FileMode.Create, FileAccess.Write)
 Using writer As New StreamWriter(fileStr)
   writer.WriteLine("Error Messages")
   For d As Integer = 0 To ar.Count - 1
      writer.WriteLine(ar(d).ToString())
      sErrMsg += "<tr><td class='errgrid'>><td class='errgrid'>" + ar(d).ToString() + "</td><tr>"
   Next
   writer.Close()
 End Using
 fileStr.Close()
End Using

I've an arraylist having 30000 items in it, what's the best way of creating a text file on the fly from an ASP.NEt page? Currently I'm using the code below but it times out with large data,

Using fileStr As New FileStream(sFileName, FileMode.Create, FileAccess.Write)
 Using writer As New StreamWriter(fileStr)
   writer.WriteLine("Error Messages")
   For d As Integer = 0 To ar.Count - 1
      writer.WriteLine(ar(d).ToString())
      sErrMsg += "<tr><td class='errgrid'>><td class='errgrid'>" + ar(d).ToString() + "</td><tr>"
   Next
   writer.Close()
 End Using
 fileStr.Close()
End Using

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

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

发布评论

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

评论(3

时间你老了 2024-09-18 23:57:39

我假设 sErrMsg 是一个字符串变量。您确实想将其更改为 StringBuilder。

I assume sErrMsg is a String variable. You really want to change that into a StringBuilder.

左耳近心 2024-09-18 23:57:39

嗯,我认为这是一个简单的方法:

Dim fs As FileStream = File.Create(path)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

        ' Add some information to the file.
        fs.Write(info, 0, info.Length)
        fs.Close()

http://msdn.microsoft.com /en-us/library/d62kzs03.aspx

问候,

Well i think this is an easy way:

Dim fs As FileStream = File.Create(path)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

        ' Add some information to the file.
        fs.Write(info, 0, info.Length)
        fs.Close()

http://msdn.microsoft.com/en-us/library/d62kzs03.aspx

Regards,

千鲤 2024-09-18 23:57:39

首先,我想说将其从 ASP .Net Page 中取出,并将其作为在服务器上运行的内容。然后在服务器上创建该文件,然后让用户下载该文件或执行您需要对其执行的任何操作。

Well first off I'd say take this out of the ASP .Net Page and put it as something that runs on the server. Then create that file on the server then just have the user download the file or do whatever you need to do with it.

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