Using Internet From C#

Is it possible to use some .NET library to download a file from the internet automatically to a specified directory Let's say I have an excel chart on my website, and I want to download it to C://

Also, I want to be able to do the same with a web page, i.e., I want to go to www.mywebpage.com/index.html and download index.html to a certain folder by using .NET.

Will this take a lot of complicated code Can I just use a .NET library, or must I use Win32 API or something like that



Answer this question

Using Internet From C#

  • Evan Mulawski

    Okay, but why would there be tags in the first place if I am requesting the image from something like www.msdn.com/logo.jpg And what's wrong with substring or trimstart and trimend for taking out the tags

    Also, about File Downloading and Uploading. In WebRequestMethods there is http, ftp, and file, how do I access file and ftp

    What is the difference between that (webrequest) and Web Client

    Also, how can I get the progress of a download or upload





  • AlexBB

    Hi xRuntime,

    you can use Image.FromStream to create an Image instance from the Stream you get as Response of your WebRequest call. Your picture box has an Image property which you assign the Image instance to.

    --
    SvenC


  • lord_8

    you could use a memorystream to save the image, then tell the PictureBox to load it from the memorystream.



  • Johanvh

    you have a couple of choices

    1) use the WebBrowser control in .NET 2.0 to navigate to a website and save the document

    2) use a WebRequest and WebResponse classes to read the Stream of data returned overall and save it to a file

    which one are you after :-)



  • Preeteesh

    1) no i didnt want that

    2) yes, thats what im after, but I somewhat accomplished it

    following these directions http://www.csharpfriends.com/Articles/getArticle.aspx articleID=115
    I can upload and download file

    however, what would be best is that instead of saving the file to the computer, accessing the file from the internet and using it dynamically in the program, that is what I want to do...

    for example, I want to access a picture from the web and display it in a picture box

    thanks



  • SterlingH

    Err...and how do I do that


  • pasha2k

    Since I can't edit messges because of the error messages, I'm making a new post.

    I've figured out how to use the event progress changed, but I cant get the actual percent of progress or file size done, how would I do that


  • Esqueleto

    so are you using a webbrowser control now to do this or something else

    the actual percentage you may have to calculate yourself depending on how many bytes were downloading and how many bytes remaining. Can you explain how you are getting and using the event progress changed



  • John Mathews

    http://msdn2.microsoft.com/en-us/library/system.io.memorystream.aspx

    http://msdn2.microsoft.com/en-us/library/system.drawing.image.aspx

    There are a few ways to do so.

    One method would be to take out the image tags and then rerequest the image so it gets the actual image itself, and read itinto the memory stream, then load the Image from the memorystream using the PictureBox.Image property (Image.FromStream)

    To strip out the tags, best way would be to use Regex (regular expressions). They are expensive and tricky to learn but usefulfor these scenario's to get the image tags and request the image and read it into the memorystream.

    The disadvantage being the fact that you have to re-request - creating the request to point to the image so doing 2 calls instead

    I'm sure there are other ways but this one just springs into mind



  • Jonas.S

    Before I proceed any furhter there is something very wrong with this forum today, everytime I tyr posting, an error occurs, everytime I try marking an answer, an error occurs. Edited posts arent saved, and when I click back the data is gone, so I lose entire posts that Ive wrritten, new threads work, but it still gives me an error.

    I get so many errors!!

    What is wrong

  • schmod54

    ok well I was thinking you were talking about just a webpage and not an image but if its an image directly then yes that makes things a whole load easier

    using trimstart and all the string stuff is very messy and can have unpredicated results somethings therefore doing Regex does things properly for you (assuming you entered the correct "formula")

    So now, since you have the image path, simply use the WebResponse/Request classes to read the image directly into a Stream , then tell the picturebox to load the image from the stream.

    WebRequest theRequest = WebRequest.Create("ImageUrl");

    WebResponse theResponse = theRequest.GetResponse();

    using (StreamReader theReader = new StreamReader(theResponse.GetResponseStream());

    {

    this.thePictureBox.Image = Image.FromStream(theReader.BaseStream);

    }



  • acf

    no worries. Yes the forum is very dodgy again today so you have to bare with us. Please do come back and mark the answers once the forums are back up.

    to get the progress bar indication, there have been a few threads here - do a forum search and you will have some hits. Take a look at the class, there should also be some examples:

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.progressbar.aspx

    If you had a WebBrowser control, things would be alot easier as there is a FileDownload event from which you can calculate easily the progress and so on but since this is not what you may be doing, you can still achieve what you are after using the resource given as well as the examples in these forums.

    2) Video streaming, im not entirely sure. WME - Windows Media Encoder - would be the best bet for streaming audio/video content from your computer. In C#, it may well be possible using the Stream classes (FileStream/MemoryStream/StreamReader/Writer) and some TCP/IP Communication (Sockets, TCPClient/TCPListener/NetworkStream classes)

    if you are using a WMP Control then simply just give it the url to the media file and it will do the rest itself.

    Hope this works for you!



  • Palvinder Singh

    No I'm using the System.Net.WebClient Class..

    All the event handlers etc. are in there, but I dunno how to even get the bytes and all that..if i did, it would be somewhat easy to get the percentage and update a progress bar..


  • Octopus384

    After doing some thorough searching, I came across this article, http://www.developerfusion.co.uk/show/4637/2/, which explains some good stuff. This answers y web page question, but how do I do files


  • Using Internet From C#