PDA

View Full Version : خواندن یک فایل متنی که در اینترنت هست؟



hamedre81
دوشنبه 15 آبان 1391, 14:47 عصر
سلام
چطوری می تونم یه فایل متنی که در اینترنت هست رو باز کنم مقدارش رو در یه متغییری ذخیره کنم؟

کامبیز اسدزاده
دوشنبه 15 آبان 1391, 15:00 عصر
WebRequest request = FileWebRequest.Create("http://www.test.com/a.txt");
<span class="highlight">WebResponse</span> response = request.GetResponse();

string LocalPath = "C:\\filename.txt";

byte[] buffer = new byte[response.ContentLength];
response.GetResponseStream().Read(buffer,0,Convert .ToInt32(response.ContentLength));

FileStream fs = new FileStream(LocalPath, FileMode.Create, FileAccess.Write);
fs.Write(buffer,0,Convert.ToInt32(buffer.Length));

response.Close();
fs.Close();

hamedre81
دوشنبه 15 آبان 1391, 18:29 عصر
من این کد شما رو استفاده کردم همش خطا داشت
شما چطوری این کد رو استفاده کردین؟

Mahmoud Zaad
دوشنبه 15 آبان 1391, 19:14 عصر
سلام
با کد زیر میتونید اینکارو انجام بدید:
WebClient wc = new WebClient();
string str = wc.DownloadString("Internet Address");
MessageBox.Show(str);
//DownLoad rss site AsrIran va zakhire dar kenar file exe-directory barname
//wc.DownloadFile("http://www.asriran.com/fa/rss/allnews", "rss.xml");