简体   繁体   中英

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();
    }
}

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.

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();
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM