返回列表 回复 发帖

Using BITS with c#

using Microsoft.Msdn.Samples.BITS;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using System.Data;

namespace demetz.NDownload
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.ListBox listBox1;
  private Microsoft.Msdn.Samples.BITS.JobCollection myJobs;
  private Microsoft.Msdn.Samples.BITS.JobEvents myEvents;
  private System.Windows.Forms.TextBox txtEvents;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  /// <summary>
  /// constructor
  /// </summary>
  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
   
   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   myEvents = new JobEvents();

   this.txtEvents = new System.Windows.Forms.TextBox();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.listBox1 = new System.Windows.Forms.ListBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // txtEvents
   //
   this.txtEvents.Name = "txtEvents";
   this.txtEvents.TabIndex = 0;
   this.txtEvents.Text = "";
   //
   // textBox1
   //
   this.textBox1.AccessibleDescription = "";
   this.textBox1.Location = new System.Drawing.Point(120, 16);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(272, 20);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   //
   // textBox2
   //
   this.textBox2.AccessibleDescription = "";
   this.textBox2.Location = new System.Drawing.Point(120, 40);
   this.textBox2.Name = "textBox2";
   this.textBox2.Size = new System.Drawing.Size(272, 20);
   this.textBox2.TabIndex = 1;
   this.textBox2.Text = "";
   //
   // textBox3
   //
   this.textBox3.AccessibleDescription = "";
   this.textBox3.Location = new System.Drawing.Point(120, 64);
   this.textBox3.Name = "textBox3";
   this.textBox3.Size = new System.Drawing.Size(272, 20);
   this.textBox3.TabIndex = 2;
   this.textBox3.Text = "";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(16, 96);
   this.button1.Name = "button1";
   this.button1.TabIndex = 3;
   this.button1.Text = "add job";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // listBox1
   //
   this.listBox1.Location = new System.Drawing.Point(16, 176);
   this.listBox1.Name = "listBox1";
   this.listBox1.Size = new System.Drawing.Size(400, 173);
   this.listBox1.TabIndex = 4;
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 144);
   this.label1.Name = "label1";
   this.label1.TabIndex = 5;
   this.label1.Text = "running jobs";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(16, 16);
   this.label2.Name = "label2";
   this.label2.TabIndex = 6;
   this.label2.Text = "name & descr";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(16, 40);
   this.label3.Name = "label3";
   this.label3.TabIndex = 7;
   this.label3.Text = "diskfilename";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(16, 64);
   this.label4.Name = "label4";
   this.label4.TabIndex = 8;
   this.label4.Text = "URL";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(464, 389);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.label4,
                    this.label3,
                    this.label2,
                    this.label1,
                    this.listBox1,
                    this.button1,
                    this.textBox3,
                    this.textBox2,
                    this.textBox1});
   
   this.Name = "Form1";
   this.Text = "Testing BITS Wrapper";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);
   this.ToolTip();
  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
   
  }

  /// <summary>
  /// click
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button1_Click(object sender, System.EventArgs e)
  {
   runBITSJob(textBox1.Text,textBox2.Text,textBox3.Text);
   // clear text
   textBox1.Text = "";
   textBox2.Text = "";
   textBox3.Text = "";
   // updates list
   UpdateList();
  }


  /// <summary>
  /// load form and updates running jobs list
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void Form1_Load(Object sender,   System.EventArgs e)
  {
   UpdateList();
  }

  /// <summary>
  /// runs job
  /// </summary>
  public void runBITSJob(string name, string diskfilename, string url)
  {
   //
   Microsoft.Msdn.Samples.BITS.Manager bitsman = new Microsoft.Msdn.Samples.BITS.Manager();
   Job bitsjob = bitsman.CreateJob(name,name);
   bitsjob.AddFile(@"C:\dl\" + diskfilename,url);
  }


  /// <summary>
  /// set up tooltips
  /// </summary>
  private void ToolTip()
  {
   // Create the ToolTip and associate with the Form container.
   ToolTip toolTip1 = new ToolTip();

   // Set up the delays for the ToolTip.
   toolTip1.AutoPopDelay = 500;
   toolTip1.InitialDelay = 500;
   toolTip1.ReshowDelay = 500;
   // Force the ToolTip text to be displayed whether or not the form is active.
   toolTip1.ShowAlways = true;
         
   // Set up the ToolTip text for the textboxes
   toolTip1.SetToolTip(this.textBox1, "sets name and description of job");
   toolTip1.SetToolTip(this.textBox2, "sets name of file as which it will be saved on disk");
   toolTip1.SetToolTip(this.textBox3, "http url of file");
   // Set up the ToolTip text for the labels
   toolTip1.SetToolTip(this.label2, "sets name and description of job");
   toolTip1.SetToolTip(this.label3, "sets name of file as which it will be saved on disk");
   toolTip1.SetToolTip(this.label4, "http url of file");
  }
  
  /// <summary>
  /// updates list of running jobs
  /// </summary>
  private void UpdateList()
  {
    Manager myBITS = new Manager();
    myJobs = myBITS.GetListofJobs(JobType.CurrentUser);
    //Job currentJob;
    foreach (Job currentJob in myJobs)
    {
     myEvents.AddJob(currentJob);
    }
    listBox1.DataSource = myJobs;
  }
}
}
返回列表