rajiv
rajiv

Reputation: 21

Creating a text file in C# for windows Mobile App

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

Answers (2)

ahmed
ahmed

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

josef
josef

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();
  }
}
  1. there are no drive letters on Windows Mobile 6.x devices
  2. there is no \users directory

Forget about the answers targetting Windows Phone or Windows Embedded 8 Handheld.

Upvotes: 1

Related Questions