2016-03-10 94 views
2

我是C#中的新成員,目前正在開發一個Windows Mobile應用程序,在該應用程序中,我必須在按鈕的單擊事件上創建文本文件,並且必須編寫存在的文本字段的值在任何人都可以幫我解決這個問題,我正在使用visual 2008。在C#中爲Windows Mobile App創建文本文件

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

之前,您應該使用本地存儲對於這一點,有沒有'C'開車去。 –

+0

這不是關於Windows Phone應用程序,因爲用戶使用VS2008。 – josef

回答

1

使用

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掌上電腦。

0

你應該關閉該文件第一次創建開放的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(); 
}} 
相關問題