Download///<summary>
Download files
</summary>
public class Bddownloadfile
{
private string Url; The file URL address to download
private string Savepath; The directory of the files to be saved
private string errmsg; Save error message
private string Regvalue = @ "http://" ([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)? ";
private string SaveFile; Generated files
Public Bddownloadfile (String url,string path)
{
url = URL;
Savepath = path;
This. SaveFile = GetFileName ();
}
<summary>
return error message
</summary>
public string errormessage
{
Get
{
return this.errmsg;
}
}
public string Getsavefile
{
Get
{
return this. SaveFile;
}
}
public bool DownLoadFile ()
{
bool result = true;
if (! Checkurl (this. URL))
{
this.errmsg = "URL is illegal! ";
return false;
}
WebClient OBJWC = new WebClient ();
Objwc.credentials = CredentialCache.DefaultCredentials;
Try
{
byte[] Tmpdata = Objwc.downloaddata (this. URL);
if (Tmpdata.length > 0)
{
FileStream OBJFS = new FileStream (this. Savefile,filemode.create);
Objfs.write (tmpdata,0, (int) tmpdata.length); Writing data to a file
Objfs.close ();
This.errmsg = "There is data!" ";
}
Else
{
result = false;
This.errmsg = "No data received!" ";
}
}
catch (System.Net.WebException e)
{
This.errmsg + = "<li> Download data error occurred!" "+ e.message;
return false;
}
catch (System.uriformatexception e)
{
This.errmsg + = "<li> access URL is invalid! "+ e.message;
return false;
}
catch (Exception e)
{
this.errmsg = "error message: <LI>." +e.message;
result = false;
}
Finally
{
Objwc.dispose ();
}
return result;
}
<summary>
Check if the URL is legal
</summary>
<param name= "Chkurl" > to check the URL </param>
<returns></returns>
private bool Checkurl (string chkurl)
{
Regex reg = new regex (regvalue);
Match match = Reg. Match (Chkurl);
Return match. Success;
}
<summary>
To obtain a file URL, if there are files, then processing, if not, then return. html
</summary>
<param name= "Chkurl" ></param>
<returns></returns>
private string Getfiletype (String chkurl)
{
if (Chkurl.lastindexof ("/") = = (chkurl.length-1)//No specific file
return "HTML";
int j = 0;
for (int i = 0; i < chkurl.length; i++)
{
if (Chkurl.indexof ("/", I) >-1)
{
i = Chkurl.indexof ("/", I);
j + +;
}
}
if (J < 3)
return "HTML";
Get the character after "/" and then draw the file type
String end = Chkurl.substring (Chkurl.lastindexof (".") +1);
Switch (end)
{
Case "ASP":
Case "aspx":
Case "JSP":
Case "PHP":
return "HTML";
Default
return end;
}
}
private String GetFileName ()
{
String fileName = this. Savepath + "\" + System.DateTime.Now.Year.ToString () +system.datetime.now.month.tostring () + System.DateTime.Now.Day.ToString () +system.datetime.now.minute.tostring () +system.datetime.now.second.tostring () +common.makerandom (4). ToString () + "." +getfiletype (this. URL);
for (; File.exists (fileName);)
{
FileName = this. Savepath + "\" + System.DateTime.Now.Year.ToString () +system.datetime.now.month.tostring () + System.DateTime.Now.Day.ToString () +system.datetime.now.minute.tostring () +system.datetime.now.second.tostring () +common.makerandom (4). ToString () + "\" +getfiletype (this. URL);
}
return fileName;
}
}//bddownloadfile End