Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I download video files programmatically in C# [modified]

How can I download video files programmatically in C# [modified]

Scheduled Pinned Locked Moved C#
tutorialcsharphelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    desamsetty
    wrote on last edited by
    #1

    Hi All, I have a web application and I would like to download video files i.e. .flv or .wmv files from a web site, for example, youtube or google programmatically in my c# code. If you know how to do this please respond to this message. I appreciate your help. Thanks. -- modified at 19:23 Thursday 14th September, 2006

    D S 2 Replies Last reply
    0
    • D desamsetty

      Hi All, I have a web application and I would like to download video files i.e. .flv or .wmv files from a web site, for example, youtube or google programmatically in my c# code. If you know how to do this please respond to this message. I appreciate your help. Thanks. -- modified at 19:23 Thursday 14th September, 2006

      D Offline
      D Offline
      Dominic Pettifer
      wrote on last edited by
      #2

      Are you saying you want to offer video files to be viewed on your website, direct from your web servers hard drive? Or a way to retrieve video files from google video and YouTube to be used in your application some how?

      Sunday Ironfoot www.dominicpettifer.co.uk (work in progress)

      1 Reply Last reply
      0
      • D desamsetty

        Hi All, I have a web application and I would like to download video files i.e. .flv or .wmv files from a web site, for example, youtube or google programmatically in my c# code. If you know how to do this please respond to this message. I appreciate your help. Thanks. -- modified at 19:23 Thursday 14th September, 2006

        S Offline
        S Offline
        scott_hackett
        wrote on last edited by
        #3

        Similar to something I posted a few messages up... you'll want to modify this to be a bit more asynchronous, especially if you're downloading large files like videos. This function downloads the contents of the web file and stores it in a temp file and returns the temp file name.

        public string GetWebFile(string url)
        {
        HttpWebRequest webRequest = null;
        HttpWebResponse webResponse = null;
        Stream responseStream = null;
        FileStream fileStream = null;
        string filename = "";

        try
        {
            // Create the web request
            webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            // Get the response
            webResponse = (HttpWebResponse)webRequest.GetResponse();
            // Get the response stream
            responseStream = webResponse.GetResponseStream();
            // write it to a temporary file in 1000 byte chunks
            filename = Path.GetTempFileName();
            fileStream = new FileStream(filename, FileMode.Append);
            int bytesRead = 0;
            byte\[\] data = new byte\[1000\];
            // keep looping until we read nothing
            do
            {
                // read the next 1000 bytes of data
                bytesRead = responseStream.Read(data, 0, 1000);
                // write the data to the file
                fileStream.Write(data, 0, bytesRead);
            }
            while (bytesRead > 0);
        }
        catch (Exception e)
        {
            MessageBox.Show("Error : " + e.Message);
        }
        finally
        {
            // free any resources
            if (fileStream != null)
            {
                fileStream.Close();
                fileStream.Dispose();
            }
            if (responseStream != null)
            {
                responseStream.Close();
                responseStream.Dispose();
            }
            if (webResponse != null)
            {
                webResponse.Close();
            }
        }
        
        // return the contents
        return filename;
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups