简体   繁体   English

在 C# 中为 windows Mobile App 创建文本文件

[英]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.我是 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();
    }
}

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 Windows Mobile 6.x 设备上没有驱动器号
  2. there is no \\users directory没有 \\users 目录

Forget about the answers targetting Windows Phone or Windows Embedded 8 Handheld.忘记针对 Windows Phone 或 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();
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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