C#让StreamWriter写入文本文件
我已经多次复习教授的讲座,复习课本,在网上无休止地搜索答案,并尝试了所有合理的建议解决方案。我肯定错过了一些东西。通讯簿将写入列表框,创建文件,但名称和地址不会写入文件。对于我所缺少的任何帮助,我们将不胜感激C#让StreamWriter写入文本文件,c#,windows,forms,C#,Windows,Forms,我已经多次复习教授的讲座,复习课本,在网上无休止地搜索答案,并尝试了所有合理的建议解决方案。我肯定错过了一些东西。通讯簿将写入列表框,创建文件,但名称和地址不会写入文件。对于我所缺少的任何帮助,我们将不胜感激 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sy
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
namespace LAB7A
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Person> people = new List<Person>();
class Person
{
public string Name
{
get;
set;
}
public string Email
{
get;
set;
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
StreamWriter address = new StreamWriter("addressbook.txt");
address.WriteLine(textBox1.Text, textBox2.Text);
}
catch (DirectoryNotFoundException exc)
{
MessageBox.Show("Directory Could Not Be Found");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Person p = new Person();
p.Name = textBox1.Text;
p.Email = textBox2.Text;
people.Add(p);
listBox1.Items.Add(p.Name);
listBox1.Items.Add(p.Email);
//StreamWriter address.WriteLine(textBox1.Text);
//address.WriteLine(textBox2.Text);
//listBox1.Items.Add(textBox1.Text);
//listBox1.Items.Add(textBox2.Text);
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用制度;
使用系统图;
使用系统集合;
使用系统组件模型;
使用系统数据;
使用System.IO;
名称空间LAB7A
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
列表人员=新列表();
班主任
{
公共字符串名
{
收到
设置
}
公共字符串电子邮件
{
收到
设置
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
尝试
{
StreamWriter地址=新的StreamWriter(“addressbook.txt”);
address.WriteLine(textBox1.Text,textBox2.Text);
}
捕获(DirectoryNotFoundException exc)
{
Show(“找不到目录”);
}
}
私有无效按钮1\u单击(对象发送者,System.EventArgs e)
{
人员p=新人员();
p、 Name=textBox1.Text;
p、 Email=textBox2.Text;
新增(p);
列表框1.Items.Add(p.Name);
列表框1.Items.Add(p.Email);
//StreamWriter address.WriteLine(textBox1.Text);
//address.WriteLine(textBox2.Text);
//listBox1.Items.Add(textBox1.Text);
//listBox1.Items.Add(textBox2.Text);
}
私有无效列表框1\u SelectedIndexChanged(对象发送方,System.EventArgs e)
{
}
}
}
尝试在流上调用Flush或Close。最好的做法是将其包装在一个有用的文件中
using(StreamWriter address = new StreamWriter("addressbook.txt"))
{
...
}
向下滚动到示例代码。如果忘记添加完整地址,请尝试以下操作:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
using (StreamWriter address = new StreamWriter("d:\\addressbook.txt"))//Add correct address
{
address.WriteLine(textBox1.Text + textBox2.Text);
//OR
address.WriteLine(string.Format("Name:{0}, Email:{1}", textBox1.Text, textBox2.Text));
}
}
catch (DirectoryNotFoundException exc)
{
MessageBox.Show("Directory Could Not Be Found");
}
}
当所有字段都为空时,您正在表单加载时将数据保存到文件中。有机会在文本字段中键入值后,将编写代码移动到某个按钮单击处理程序 您还使用了将format作为第一个参数的方法版本。除非您真的想这样做,否则两个单独的
WriteLine
调用或一个具有正确格式字符串的调用可能会满足您的需要
还要确保关闭/处置流写入程序,以便它提交对文件的更改并关闭文件。通常的方法是使用使用:
using (var writer = new StreamWriter("addressbook.txt"))
{
writer.WriteLine("First textbox:{0}, second:{1}", textBox1.Text, textBox2.Text);
}
这真的很有帮助。但是,我需要它来写用户在文本框中输入的内容,而我尝试的内容失败了。如果这是初级的,请原谅,我只是在学c。这就是我正在使用的,虽然它不会抛出任何错误,但它不会在文本框.address.WriteLine(textBox1.text+textBox2.text)中写入用户输入的姓名和电子邮件地址;而不是address.WriteLine(“asd”)代码>使用地址.WriteLine(textBox1.Text+textBox2.Text)
或address.WriteLine(string.Format(“名称:{0},电子邮件:{1}”,textBox1.Text,textBox2.Text))代码>这与我上面发布的有什么不同?我相信这正是我所拥有的,它仍然没有将这些信息写入文件,我只是尝试了string.Format(“Name:{0},Email:{1}”,textBox1.Text,textBox2.Text,我得到的唯一输出是写下的姓名和电子邮件,这显然是因为它是一个字符串。问题是,它不会在单击“添加联系人”按钮时写入文本框中的信息。非常重要的是,最后一位-StreamWriter
很可能在之前不会刷新其内容>Flush
被显式调用或对象被释放。将其包装在using语句中可确保写入内容并关闭流(在写入完成后关闭流也可防止进程锁定文件)