[英]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();
}
}
忘記針對 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.