ex: "http://tw.yahoo.com/index.htm"
儲存檔案位址 本機儲存位址
ex: "c:\download\index.htm"
using System; using System.IO; using System.Text; using System.Net; static void get_file(string[] args) { try { string fn = "超連結"; HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(fn); HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Console.WriteLine("File size = " + httpResponse.ContentLength); System.IO.Stream datastream = httpResponse.GetResponseStream(); byte[] buffer = new byte[8192]; FileStream fs = new FileStream("本機儲存位址", FileMode.Create, FileAccess.Write); int size = 0; do { size = datastream.Read(buffer, 0, buffer.Length); if (size > 0) fs.Write(buffer, 0, size); } while (size > 0); Console.WriteLine(fn + " download Done!!"); fs.Close(); httpResponse.Close(); } catch(Exception) { Console.WriteLine("Url not exists!"); } Console.ReadKey(); }
沒有留言:
張貼留言