VB.NET 写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17185484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:58:01  来源:igfitidea点击:

VB.NET write to file

asp.netvb.netstreamwriter

提问by Selrac

This should be an easy one, but I'm strugling.

这应该是一件容易的事,但我正在挣扎。

I've developed a web page and I'm trying to load values into a text file. I have the asp webform with a textbox and a button. When the button is pressed loads the message from the textbox to the text file.

我开发了一个网页,我正在尝试将值加载到文本文件中。我有一个带有文本框和按钮的 asp 网络表单。当按下按钮时,将消息从文本框加载到文本文件中。

When debugg it, it appears to work, but I can not see anything written in the file when I open it (or maybe I'm looking in the wrong place?)

调试它时,它似乎可以工作,但是当我打开它时我看不到文件中写入的任何内容(或者我可能找错了地方?)

When I publish it, it does not work.

当我发布它时,它不起作用。

Here is the code I'm using

这是我正在使用的代码

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

    ' Both file_name below seem to work
    ' Dim FILE_NAME As String = "TB.txt"
    Dim FILE_NAME As String = "..\TB.txt"
    Dim line1 As String

    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
    'objWriter.NewLine = True

    line1 = TextBox1.Text

    objWriter.WriteLine(line1)
    'objWriter.Write(TextBox1.Text)

    objWriter.Close()
    MsgBox("Text written to file")

End Sub

Any help will be much appreciated

任何帮助都感激不尽

回答by SysDragon

Try this:

尝试这个:

IO.File.WriteAllText(FILE_NAME, TextBox1.Text)

MSDN Documentation

MSDN 文档

回答by Selrac

I found that the problem was created by this line:

我发现问题是由这一行造成的:

MsgBox("Text written to file")

Once this was removed all worked fine.

一旦删除所有工作正常。

Thanks for all the advice and support

谢谢大家的建议和支持