class Program
{
static void Main(string[] args)
{
string dir = "c:\\";
//設定資料夾路徑
string folder = "myFolder";
//設定資料夾名稱
string file = "mytext.txt";
//設定檔案名稱
string path = System.IO.Path.Combine(dir,folder);
//路徑加入資料夾名稱
if (!System.IO.Directory.Exists(path))
{
//判斷資料夾是否存在
System.IO.Directory.CreateDirectory(path);
//建立資料夾
System.Console.WriteLine("CreateDirectory Success");
}
path = System.IO.Path.Combine(path,file);
//路徑加入檔案名稱
if (!System.IO.File.Exists(path))
{
//判斷檔案是否存在
using (StreamWriter sw = System.IO.File.CreateText(path))
{
//新增檔案並寫入資料
sw.Write("hello", Encoding.UTF8);
sw.Close();
}
System.Console.WriteLine("CreateFile Success");
}
else
{
using (StreamReader sr = System.IO.File.OpenText(path))
{
//開啟檔案並讀取資料
System.Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
}
Console.ReadKey();
}
}
2012年9月1日 星期六
C# Creat Write Read File
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言