string path = "C:\\test.txt";
if (File.Exists(path)) ///如果檔案存在,那麼删除檔案
File.Delete(path);
FileStream fs = File.Open(path, FileMode.Create); ///這裡第二個表示如果檔案不存在,則重建立立一個新的
///FileStream fs = File.Open(path, FileMode.Append); ///如果要追加内容時用這個
fs.Write(bs, 0, bs.Length); ///這裡的bs是一個數組byte[]
fs.Close();
string str = "C:\\test.txt";
if (!File.Exists(str)) ///檢測檔案是否存在
{
MessageBox.Show("檔案不存在,請檢視用戶端是否已經上傳資料!");
}
else
{
FileStream fop = File.OpenRead(str);
byte[] arr = new byte[1000];
while (fop.Read(arr, 0, arr.Length) > 0) ///這個循環會将檔案所有的内容都讀取出來
{
ClientSocket[1].Send(arr, 0, arr.Length,0);
}
fop.Close();
}
string path="C:\\TEST.txt";
string st = "";
if (!File.Exists(path))
MessageBox.Show("檔案不存在,請先建立一個!");
else
{
byte[] b=new byte[10];
StreamReader sr = new StreamReader(path);
while ((st = sr.ReadLine()) != null)
{
tb.AppendText(st);
MessageBox.Show(st);
}
sr.Close();
}
if (File.Exists(path))
File.Delete(path);
StreamWriter sw = new StreamWriter(path,true); /// true表示檔案存在就将内容加在後面,false表示覆寫内容
sw.Write("today is a good day!");
sw.Close();
binaryreader提供了readstring,readbyte,readboolean,readint,readdouble等方法來讀取不同類型的資料,而binarywriter則提供write()方法将各種不同類型的值寫入目前流(也就是write方法的參數可以是任何基本資料類型)。下面以存儲和讀取double類型的資料來舉例。
FileStream fs=File.Create(path1);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(100.123);
bw.Write(200.524);
bw.Close();
fs.Close();
FileStream fs1 = new FileStream(path1, FileMode.Open);
BinaryReader br = new BinaryReader(fs1);
Console.WriteLine("{0}",br.ReadDouble());
Console.WriteLine("{0}", br.ReadDouble());
br.Close();
fs.Close();