Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download fle from Internet in Windows App 2

Status
Not open for further replies.

DarwinIT

Programmer
Apr 25, 2008
142
US
How can I do this? Httprequest, according to what I'm reading, only works in a web environment. I need to automate the updating of tax information from the state for a client.
 
Thanks guys. This was surprisingly easy.
(For a change, something on MSDN actually helped me!!)

I had a problem in that it was a zip file and was corrupted. I switched to writing a byte array instead of a string - and I'm good to go!!
Thanks again.


using System;
using System.Net;
using System.IO;

public class Test
{
public bool RetrieveFile(string fileURL,string fileLocation)
{

try
{
WebClient client = new WebClient();

// Add a user agent header in case the
// requested URI contains a query.


client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead(fileURL);
// StreamReader reader = new StreamReader(data);
// StreamWriter writer = new StreamWriter(fileLocation,false);

/*
string s = reader.ReadToEnd();
writer.WriteLine(s);
data.Close();
reader.Close();
writer.Close();
return true;
*/

int offset = 0;
byte[] buffer = new byte[1000];
int size;
FileStream fileStream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write);
int count = buffer.Length;
do
{

// read into buffer
size = data.Read(buffer, offset, count);
// write into the filestream
fileStream.Write(buffer, offset, size);
} while (size > 0);


data.Close();
fileStream.Flush();
fileStream.Close();
return true;

}
catch (Exception ex)
{
string errorMessage = ex.Message;
return false;
}

}
}
 
i would make 1 suggestion.
Code:
try
{
   WebClient client = new WebClient();
   client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

   using(Stream data = client.OpenRead(fileURL))
   {
      int offset = 0;
      byte[] buffer = new byte[1000];
      int size;
      using(FileStream fileStream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write))
      {
         int count = buffer.Length;
         do
         {
            size = data.Read(buffer, offset, count);
            fileStream.Write(buffer, offset, size);
         } while (size > 0);
         fileStream.Flush();
      }
   }
   return true;
}
catch (Exception ex)
{
   string errorMessage = ex.Message;
   return false;
}
the using statements will automatically close/dispose the streams if an exception is thrown. with your code above they streams would not be closed/disposed.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top