用webclient实现,using System.net string remoteUri = "http://10.234.188.92/update/"; string fileName = "swtest.txt", myStringWebResource = null; // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Concatenate the domain with the Web resource filename. myStringWebResource = remoteUri + fileName; Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource); // Download the Web resource and save it into the current filesystem folder. myWebClient.DownloadFile(myStringWebResource,"c:\\"+fileName); Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
string remoteUri = "http://10.234.188.92/update/";
string fileName = "swtest.txt", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,"c:\\"+fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
int n = URL.LastIndexOf('/');
string URLAddress = URL;//.Substring(0,n);
string fileName = URL.Substring(n+1,URL.Length-n-1);
string Dir = strTargetPath;
string Path = Dir+'\\'+fileName; try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
return exp.Message;
} try
{
//"开始下载文件...";
client.DownloadFile(URLAddress,fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[1000000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
//"正在接收数据...";
while(allmybyte>0)
{
int m = str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break; startmbyte+=m;
allmybyte-=m;
} FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate ,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();
}
catch(WebException exp)
{
return exp.Message;
// "";
} return "";