繁体   English   中英

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

[英]Creating a text file in C# for windows Mobile App

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

使用那个

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

忘记针对 Windows Phone 或 Windows Embedded 8 Handheld 的答案。

您应该在打开流编写器之前先关闭文件创建

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