// Libraries used in this class: using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Diagnostics; using System.Windows.Forms; using System.Linq; using System.Xml.Linq; using System.Net; using System.IO; namespace YouTube_Getter { public partial class mainForm : Form { //internal mainForm() public mainForm() { InitializeComponent(); } private byte[] downloadedDataStream; private Video_Downloader_DLL.VideoDownloader downloader = new Video_Downloader_DLL.VideoDownloader(); private void Button1_Operation(object sender, System.EventArgs e) { string videoHeader = ""; //string thumbnailurl = ""; string outputStr = ""; string fileinputurl = TextBox1.Text; downloader.GetvideoHeader(fileinputurl, ref videoHeader); System.String tempVar = ""; System.Int32 tempVar2 = 0; downloader.MakeDownloadURL(fileinputurl, ref outputStr, ref tempVar, ref tempVar2); //downloader.GetPreviewThumbnail(fileinputurl, ref thumbnailurl); OutLink.Text = (videoHeader); LinkText.Text = (outputStr); //PicImage.Text = (thumbnailurl); MessageBox.Show("To Download " + videoHeader + Environment.NewLine + "Please click Download now, When finished Downloading select Save To File"); } private void downloadData(string url) { progressBar1.Value = 0; downloadedDataStream = new byte[0]; try { //Optional this.Text = "Connecting..."; Application.DoEvents(); //Get a data stream from the url WebRequest req = WebRequest.Create(url); WebResponse response = req.GetResponse(); Stream stream = response.GetResponseStream(); //Download in chuncks byte[] buffer = new byte[1024]; //Get Total Size int dataLength = (int)response.ContentLength; //With the total data we can set up our progress indicators progressBar1.Maximum = dataLength; lbProgress.Text = "0/" + dataLength.ToString(); this.Text = "Downloading..."; Application.DoEvents(); //Download to memory //Note: adjust the streams here to download directly to the hard drive MemoryStream memStream = new MemoryStream(); while (true) { //Try to read the data int bytesRead = stream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { //Finished downloading progressBar1.Value = progressBar1.Maximum; lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString(); Application.DoEvents(); break; } else { //Write the downloaded data memStream.Write(buffer, 0, bytesRead); //Update the progress bar if (progressBar1.Value + bytesRead <= progressBar1.Maximum) { progressBar1.Value += bytesRead; lbProgress.Text = progressBar1.Value.ToString() + "/" + dataLength.ToString(); progressBar1.Refresh(); Application.DoEvents(); } } } //Convert the downloaded stream to a byte array downloadedDataStream = memStream.ToArray(); //Clean up stream.Close(); memStream.Close(); } catch (Exception) { //May not be connected to the internet //Or the URL might not exist MessageBox.Show("There was an error accessing the URL."); } txtData.Text = downloadedDataStream.Length.ToString(); this.Text = "YT Snatcher"; } private void btnDownload_Click(object sender, EventArgs e) { downloadData(LinkText.Text); //Get the last part of the url, ie the file name if (downloadedDataStream != null && downloadedDataStream.Length != 0) { string ytdata = OutLink.Text; string urlName = LinkText.Text; if (urlName.EndsWith("/")) urlName = urlName.Substring(0, urlName.Length - 1); //Chop off the last '/' urlName = urlName.Substring(urlName.LastIndexOf('/') + 1); saveDiag1.FileName = ytdata + ".flv"; } } private void button2_Click(object sender, EventArgs e) { if (downloadedDataStream != null && downloadedDataStream.Length != 0) { if (saveDiag1.ShowDialog() == DialogResult.OK) { this.Text = "Saving your file ..."; Application.DoEvents(); //Write the bytes to a file FileStream newFile = new FileStream(saveDiag1.FileName, FileMode.Create); newFile.Write(downloadedDataStream, 0, downloadedDataStream.Length); newFile.Close(); this.Text = "Download Data"; MessageBox.Show("Saved the file Successfully"); } } else MessageBox.Show("No File was Downloaded !"); } } } Listing 2. The mainForm class // Libraries used in this class: using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Diagnostics; using System.Windows.Forms; using System.Linq; using System.Xml.Linq; namespace YouTube_Getter { public partial class mainForm : System.Windows.Forms.Form { //Form overrides dispose to clean up the component list. [System.Diagnostics.DebuggerNonUserCODE()] protected override void Dispose(bool disposeWindow) { try { if (disposeWindow && components != null) { components.Dispose(); } } finally { base.Dispose(disposeWindow); } } //Required by the Windows Form Designer private System.ComponentModel.IContainer components; //NOTE: The following procedure is required by the Windows Form Designer //It can be modified using the Windows Form Designer. //Do not modify it using the code editor. [System.Diagnostics.DebuggerStepThrough()] private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm)); this.OutLink = new System.Windows.Forms.TextBox(); this.Button1 = new System.Windows.Forms.Button(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.LinkText = new System.Windows.Forms.TextBox(); this.txtData = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.lbProgress = new System.Windows.Forms.Label(); this.btnDownload = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.label7 = new System.Windows.Forms.Label(); this.saveDiag1 = new System.Windows.Forms.SaveFileDialog(); this.SuspendLayout(); // // OutLink // this.OutLink.Location = new System.Drawing.Point(278, 32); this.OutLink.Name = "OutLink"; this.OutLink.Size = new System.Drawing.Size(289, 20); this.OutLink.TabIndex = 0; this.OutLink.Visible = false; // // Button1 // this.Button1.BackColor = System.Drawing.SystemColors.Control; this.Button1.Location = new System.Drawing.Point(79, 74); this.Button1.Name = "Button1"; this.Button1.Size = new System.Drawing.Size(75, 23); this.Button1.TabIndex = 1; this.Button1.Text = "Get File Info"; this.Button1.UseVisualStyleBackColor = false; this.Button1.Click += new System.EventHandler(this.Button1_Click); // // TextBox1 // this.TextBox1.Location = new System.Drawing.Point(144, 5); this.TextBox1.Name = "TextBox1"; this.TextBox1.Size = new System.Drawing.Size(423, 20); this.TextBox1.TabIndex = 2; // // LinkText // this.LinkText.Location = new System.Drawing.Point(12, 32); this.LinkText.Name = "LinkText"; this.LinkText.Size = new System.Drawing.Size(240, 20); this.LinkText.TabIndex = 4; this.LinkText.Visible = false; // // txtData // this.txtData.BackColor = System.Drawing.SystemColors.Control; this.txtData.Location = new System.Drawing.Point(123, 116); this.txtData.Name = "txtData"; this.txtData.Size = new System.Drawing.Size(444, 20); this.txtData.TabIndex = 12; this.txtData.Text = "0"; // // label4 // this.label4.AutoSize = true; this.label4.BackColor = System.Drawing.Color.Gold; this.label4.Location = new System.Drawing.Point(12, 5); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(123, 13); this.label4.TabIndex = 13; this.label4.Text = "Put the YouTube Url Here"; // // progressBar1 // this.progressBar1.BackColor = System.Drawing.SystemColors.Control; this.progressBar1.ForeColor = System.Drawing.Color.LawnGreen; this.progressBar1.Location = new System.Drawing.Point(123, 153); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(311, 23); this.progressBar1.TabIndex = 14; // // label5 // this.label5.AutoSize = true; this.label5.BackColor = System.Drawing.Color.Gold; this.label5.Location = new System.Drawing.Point(12, 153); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(67, 13); this.label5.TabIndex = 15; this.label5.Text = "PROGRESS"; // // label6 // this.label6.AutoSize = true; this.label6.BackColor = System.Drawing.Color.Gold; this.label6.Location = new System.Drawing.Point(9, 119); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(101, 13); this.label6.TabIndex = 16; this.label6.Text = "DATA IS IN THE MEMORY"; // // lbProgress // this.lbProgress.AutoSize = true; this.lbProgress.Location = new System.Drawing.Point(440, 159); this.lbProgress.Name = "lbProgress"; this.lbProgress.Size = new System.Drawing.Size(24, 13); this.lbProgress.TabIndex = 17; this.lbProgress.Text = "0/0"; // // btnDownload // this.btnDownload.BackColor = System.Drawing.SystemColors.Control; this.btnDownload.Location = new System.Drawing.Point(315, 69); this.btnDownload.Name = "btnDownload"; this.btnDownload.Size = new System.Drawing.Size(119, 23); this.btnDownload.TabIndex = 18; this.btnDownload.Text = "START DOWNLOAD NOW ..."; this.btnDownload.UseVisualStyleBackColor = false; this.btnDownload.Click += new System.EventHandler(this.btnDownload_Click); // // button2 // this.button2.BackColor = System.Drawing.SystemColors.Control; this.button2.Location = new System.Drawing.Point(462, 69); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(105, 23); this.button2.TabIndex = 19; this.button2.Text = "SAVE IN TO FILE"; this.button2.UseVisualStyleBackColor = false; this.button2.Click += new System.EventHandler(this.button2_Click); // // label7 // this.label7.AutoSize = true; this.label7.BackColor = System.Drawing.Color.Gold; this.label7.Location = new System.Drawing.Point(12, 79); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(44, 13); this.label7.TabIndex = 20; this.label7.Text = "STEP 1"; // // mainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.DarkSlateGray; this.ClientSize = new System.Drawing.Size(579, 199); this.Controls.Add(this.label7); this.Controls.Add(this.button2); this.Controls.Add(this.btnDownload); this.Controls.Add(this.lbProgress); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.progressBar1); this.Controls.Add(this.label4); this.Controls.Add(this.txtData); this.Controls.Add(this.LinkText); this.Controls.Add(this.TextBox1); this.Controls.Add(this.Button1); this.Controls.Add(this.OutLink); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "mainForm"; this.Text = "YOUTUBE DOWNLOADER - Downloads files from you tube"; this.ResumeLayout(false); this.PerformLayout(); } internal System.Windows.Forms.TextBox OutLink; internal System.Windows.Forms.Button Button1; internal System.Windows.Forms.TextBox TextBox1; internal System.Windows.Forms.TextBox LinkText; internal System.Windows.Forms.TextBox txtData; private Label label4; private ProgressBar progressBar1; private Label label5; private Label label6; private Label lbProgress; private Button btnDownload; private Button button2; private Label label7; private SaveFileDialog saveDiag1; } } //end of root namespace Read more: http://mrbool.com/how-to-create-a-youtube-downloader-using-c/32192#ixzz4ymyEjzlZ

Comments