簡體   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