将文本框数据写入文本文件
我的代码应该获取程序运行后输入的所有用户数据并将其全部放入文本文件中。
这是到目前为止的代码:
protected void WriteFile(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\Users\4567\MyDocuments\ExporterOutput.txt", FileMode.OpenOrCreate, FileAccess.Write);
TextBox[] tbs = { username, TextBox2, sgml, Path };
StringBuilder sb = new StringBuilder();
foreach (TextBox tb in tbs)
sb.AppendLine(tb.Text);
sb.AppendLine(DropDownList1.SelectedItem.ToString());
sb.AppendLine(DropDownList2.SelectedItem.ToString());
System.IO.File.WriteAllText(@"C:/Users/4567/My Documents/ExporterOutput.txt", sb.ToString());
我尝试运行它,但文本文件仅显示为空白。谁能告诉我我做错了什么,以及是否有更简单的方法将所有文本框信息输出到文本文件。并且最好以某种格式。
这是根据您给我的建议编辑的代码:
protected void WriteFile(object sender, EventArgs e)
{
TextBox[] tbs = { username, TextBox2, sgml, Path };
StringBuilder sb = new StringBuilder();
foreach (TextBox tb in tbs)
sb.AppendLine(tb.Text);
sb.AppendLine(DropDownList1.SelectedItem.ToString());
sb.AppendLine(DropDownList2.SelectedItem.ToString());
System.IO.File.WriteAllText(@"C:\\Users\\oZ012D\\My Documents\\ExporterOutput.txt", sb.ToString());
}
I have code that is supposed to take all the user data that was input after the program was run and put it all into a text file.
Here is the code so far:
protected void WriteFile(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\Users\4567\MyDocuments\ExporterOutput.txt", FileMode.OpenOrCreate, FileAccess.Write);
TextBox[] tbs = { username, TextBox2, sgml, Path };
StringBuilder sb = new StringBuilder();
foreach (TextBox tb in tbs)
sb.AppendLine(tb.Text);
sb.AppendLine(DropDownList1.SelectedItem.ToString());
sb.AppendLine(DropDownList2.SelectedItem.ToString());
System.IO.File.WriteAllText(@"C:/Users/4567/My Documents/ExporterOutput.txt", sb.ToString());
I tried running it and the text file just shows up blank. Can anyone tell me what I am doing wrong and if there is an easier way to output all textbox information to a text file. And preferrable in a certain format.
Here is the edited code from the suggestions you gave me:
protected void WriteFile(object sender, EventArgs e)
{
TextBox[] tbs = { username, TextBox2, sgml, Path };
StringBuilder sb = new StringBuilder();
foreach (TextBox tb in tbs)
sb.AppendLine(tb.Text);
sb.AppendLine(DropDownList1.SelectedItem.ToString());
sb.AppendLine(DropDownList2.SelectedItem.ToString());
System.IO.File.WriteAllText(@"C:\\Users\\oZ012D\\My Documents\\ExporterOutput.txt", sb.ToString());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我将在这里进行一些猜测。
你的最后一行应该是
You are using windows, so you need the "\" char 而不是像 Linux 上那样的正斜杠。这是通过在字符串中使用双反斜杠(c# 将其识别为单反斜杠)来实现的。
另外,尝试去掉第一行。您正在创建一个文件流,然后不使用或关闭它。我认为开放的 FS 可能会导致问题。
不用担心创建问题,如果文件不存在,WriteAllText() 调用将创建该文件,如果存在则覆盖它。
让我知道这是否有效,对我来说唯一可能不正确的事情是您从下拉菜单中获取文本的方式。
Ok, Im going to take a few guesses here.
Your last line should be
You are using windows, so you need the "\" char and not the forward slash like you would on Linux. This is accomplished by having the double backslash (which c# recognizes as a single backslash) in your string.
Also, try getting rid of your first line. You are creating a file stream and then not using or closing it. I think its possible that the open FS is causing issues.
Don't worry about creation issues, the WriteAllText() call will create the file if it doesn't exist and will overwrite it if it does.
Let me know if that works, the only other thing that looks potentially incorrect to me is the way you are getting text from the drop down menus.