最簡單的:
--------寫
//content是要寫入文本的字串
//(@txtPath + @"\" + rid + ".txt");要被寫入的TXT
StreamWriter sw = new StreamWriter(@txtPath + @"\" + rid + ".txt");
sw.WriteLine(content);
sw.Close();
---------讀
//folder被讀取的檔案路徑
text = File.ReadAllText(folder, System.Text.Encoding.GetEncoding("gb2312"));
-------寫入流
//將string寫入檔案
try
{
FileStream fs = new FileStream(@"d:\資料處理\法規20080116HTML\" + Actid.ToString() + ".html", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.Write(pageContent);
sw.Close();
}
catch (Exception ex) { }
//HTTP嚮應後寫出檔案
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
WebResponse result = null;
try
{
myWebRequest = (HttpWebRequest)WebRequest.Create(Url);
result = myWebRequest.GetResponse();
Stream response = result.GetResponseStream();
byte[] bytes = new byte[10240];
int n = 1;
FileStream fs = File.Create(@"d:\資料處理\法規20080116HTML\" + Actid.ToString() + ".html");
while (n > 0)
{
n = response.Read(bytes, 0, 10240);
fs.Write(bytes, 0, n);
}
response.Close();
fs.Close();
}
catch (Exception ex) { }
//讀取
string FileContent = File.ReadAllText(path, System.Text.Encoding.GetEncoding("gb2312"));