Reputation: 21
I am new in C# and currently developing a windows mobile app in which i have to create a text file on click event of a button and have to write the values of text fields present in the page.Can anyone help me out with this, I'm using visual 2008 for this.
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();
}
}
Upvotes: 2
Views: 731
Reputation: 3
You should close the file create first before open 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();
}}
Upvotes: 0
Reputation: 5959
Use that
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();
}
}
Forget about the answers targetting Windows Phone or Windows Embedded 8 Handheld.
Upvotes: 1