1. Formulario con 1 progress bar, 1 Background worker, 6 TextLabels, 1 Boton.
2. label1=tc label2=tv
3. Este es el codigo que use.
using System;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Forms;
namespace GetAndSetTC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string vCompra, vVenta, fDia = "";
string webRSS = "http://www.bcb.gob.bo/rss_bcb.php";
private void button1_Click(object sender, EventArgs e)
{
if (hostAlive() == true)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
System.Net.WebRequest myRequest = System.Net.WebRequest.Create(webRSS);
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream r_stream = myResponse.GetResponseStream();
System.Xml.XmlDocument r_doc = new System.Xml.XmlDocument();
r_doc.Load(r_stream);
System.Xml.XmlNodeList r_items = r_doc.SelectNodes("rss/channel/item");
System.Xml.XmlNode tctv;
tctv = r_items.Item(0).SelectSingleNode("description");
if (tctv != null)
{
string[] temp = tctv.InnerText.Split(';');
fDia = temp[0].Replace("Fecha 1:", "");
vVenta = temp[1].Replace("VENTA Bs", "");
vCompra = temp[2].Replace("COMPRA Bs", "");
}
Int64 iSize = myResponse.ContentLength;
Int64 iRunningByteTotal = 0;
WebClient client = new WebClient();
Stream streamRemote = client.OpenRead(new Uri(webRSS));
int iByteSize = 0;
byte[] byteBuffer = new byte[iSize];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
iRunningByteTotal += iByteSize;
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
backgroundWorker1.ReportProgress(iProgressPercentage);
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
this.fecha.Text = fDia.Replace("Fecha:","");
this.tc.Text = vCompra;
this.tv.Text = vCompra;
}
/////Esta parte del codigo verifica si www.bcb.gob.bo esta online mediante un ping.
private static bool hostAlive()
{
Ping ping = new Ping();
string comp = "www.bcb.gob.bo";
try
{
PingReply reply = ping.Send(comp, 100);
if (reply.Status == IPStatus.Success)
{
return true;
}
return true;
}
catch
{
return false;
}
}
}
}
No hay comentarios:
Publicar un comentario