我是C#的新手,目前正在开发一个Windows移动应用程序,我必须在按钮的单击事件上创建一个文本文件,并且必须编写页面中存在的文本字段的值。任何人都可以帮助我这个,我正在使用visual 2008。
private void btnSubmit_Click(object sender, EventArgs e)
{
string path = "C:\\Users\\Mytext.txt";
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The very first line!");
tw.Close();
}
else if (File.Exists(path))
{
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The next line!");
tw.Close();
}
}
答案 0 :(得分:1)
使用
private void btnSubmit_Click(object sender, EventArgs e)
{
string path = "\\My Documents\\Mytext.txt";
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The very first line!");
tw.Close();
}
else if (File.Exists(path))
{
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The next line!");
tw.Close();
}
}
忘记针对Windows Phone或Windows Embedded 8掌上电脑的答案。
答案 1 :(得分:0)
您应该在打开streamwriter之前先关闭文件创建
private void btnSubmit_Click(object sender, EventArgs e){
string path = "C:\\Users\\Mytext.txt";
if (!File.Exists(path))
{
File.Create(path).close();
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The very first line!");
tw.Close();
}
else if (File.Exists(path))
{
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The next line!");
tw.Close();
}}