|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
既然话题已经抄起,我打算今晚发篇博文再引导一下舆论方向,使它再火两天,抛砖引玉,而且赵劼先生一直在跟帖,使.NET阵营的我感到万分难得。visual|下载usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Net;
usingSystem.IO;
usingSystem.Threading;
namespaceMyGetCar
{
///
///Form1的择要申明。
///
publicclassForm1:System.Windows.Forms.Form
{
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.Labellabel2;
privateSystem.Windows.Forms.TextBoxsrcAddress;
privateSystem.Windows.Forms.TextBoxtarAddress;
privateSystem.Windows.Forms.StatusBarstatusBar;
privateSystem.Windows.Forms.ButtonStart;
privateWebClientclient=newWebClient();
///
///必须的计划器变量。
///
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
//
//Windows窗体计划器撑持所必须的
//
InitializeComponent();
//
//TODO:在InitializeComponent挪用后增加任何机关函数代码
//
}
///
///清算一切正在利用的资本。
///
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindowsFormDesignergeneratedcode
///
///计划器撑持所需的办法-不要利用代码编纂器修正
///此办法的内容。
///
privatevoidInitializeComponent()
{
this.label1=newSystem.Windows.Forms.Label();
this.label2=newSystem.Windows.Forms.Label();
this.srcAddress=newSystem.Windows.Forms.TextBox();
this.tarAddress=newSystem.Windows.Forms.TextBox();
this.statusBar=newSystem.Windows.Forms.StatusBar();
this.Start=newSystem.Windows.Forms.Button();
this.button1=newSystem.Windows.Forms.Button();
this.SuspendLayout();
//
//label1
//
this.label1.Location=newSystem.Drawing.Point(8,32);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(72,23);
this.label1.TabIndex=0;
this.label1.Text="文件地点:";
this.label1.TextAlign=System.Drawing.ContentAlignment.MiddleRight;
//
//label2
//
this.label2.Location=newSystem.Drawing.Point(8,72);
this.label2.Name="label2";
this.label2.Size=newSystem.Drawing.Size(72,23);
this.label2.TabIndex=1;
this.label2.Text="另存到:";
this.label2.TextAlign=System.Drawing.ContentAlignment.MiddleRight;
//
//srcAddress
//
this.srcAddress.Location=newSystem.Drawing.Point(80,32);
this.srcAddress.Name="srcAddress";
this.srcAddress.Size=newSystem.Drawing.Size(216,21);
this.srcAddress.TabIndex=2;
this.srcAddress.Text="";
//
//tarAddress
//
this.tarAddress.Location=newSystem.Drawing.Point(80,72);
this.tarAddress.Name="tarAddress";
this.tarAddress.Size=newSystem.Drawing.Size(216,21);
this.tarAddress.TabIndex=3;
this.tarAddress.Text="";
//
//statusBar
//
this.statusBar.Location=newSystem.Drawing.Point(0,151);
this.statusBar.Name="statusBar";
this.statusBar.Size=newSystem.Drawing.Size(312,22);
this.statusBar.TabIndex=4;
//
//Start
//
this.Start.FlatStyle=System.Windows.Forms.FlatStyle.Flat;
this.Start.Location=newSystem.Drawing.Point(216,112);
this.Start.Name="Start";
this.Start.Size=newSystem.Drawing.Size(75,24);
this.Start.TabIndex=5;
this.Start.Text="入手下手下载";
this.Start.Click+=newSystem.EventHandler(this.Start_Click);
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(312,173);
this.Controls.Add(this.button1);
this.Controls.Add(this.Start);
this.Controls.Add(this.statusBar);
this.Controls.Add(this.tarAddress);
this.Controls.Add(this.srcAddress);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MaximizeBox=false;
this.Name="Form1";
this.Text="文件下载器";
this.ResumeLayout(false);}
#endregion
///
///使用程序的主出口点。
///
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
privatevoidStartDownload()
{
Start.Enabled=false;
stringURL=srcAddress.Text;
intn=URL.LastIndexOf("?");
stringURLAddress=URL;
//stringURLAddress=URL.Substring(0,n);
stringfileName=URL.Substring(n+1,URL.Length-n-1);
stringDir=tarAddress.Text;
stringPath=Dir+fileName;
try
{
WebRequestmyre=WebRequest.Create(URLAddress);
}
catch(WebExceptionexp)
{
MessageBox.Show(exp.Message,"Error");
}
try
{
statusBar.Text="入手下手下载文件...";
client.DownloadFile(URLAddress,fileName);
Streamstr=client.OpenRead(URLAddress);
//StreamReaderreader=newStreamReader(str);,这句没有甚么需要!团体以为
byte[]mbyte=newbyte[100000];
intallmybyte=(int)mbyte.Length;
intstartmbyte=0;
statusBar.Text="正在吸收数据...";
//写进到BYTE数组中,起缓冲感化
while(allmybyte>0)
{
intm=str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;
startmbyte+=m;
allmybyte-=m;
}
FileStreamfstr=newFileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();
statusBar.Text="下载终了!";
}
catch(WebExceptionexp)
{
MessageBox.Show(exp.Message,"Error");
statusBar.Text="";
}
Start.Enabled=true;
}
privatevoidStart_Click(objectsender,System.EventArgse)
{
Threadth=newThread(newThreadStart(StartDownload));
th.Start();
}
}
}
它有很多缺点的,有兴趣可以到网上去搜索一下。于是微软有发明了“下一代”C++:C++/CLI语言,这个可以解决在.NETFramework中,托管C++产生的问题。在《程序员》杂志上,lippman和李建中合作连载介绍了C++/CLI语言。 |
|