C#下载远程文件到本地
生活随笔
收集整理的這篇文章主要介紹了
C#下载远程文件到本地
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using?System;
namespace?DownRemoteFile
{
????class?DownFile
????{
????????#region?成員變量
????????private?string?savePath;
????????private?bool?newName;
????????#endregion
????????#region?屬性
????????public?string?SavePath
????????{
????????????set?
????????????{?
????????????????savePath=value.Replace("/",?"\\");
????????????????if?(savePath.LastIndexOf("\\")?!=?savePath.Length?-?1)
????????????????????savePath?+=?"\\";
????????????}
????????????get
????????????{
????????????????return?savePath;
????????????}
????????}
????????public?bool?NewName
????????{
????????????set?{?newName?=?value;?}
????????????get?{?return?newName;?}
????????}
????????#endregion
????????#region?構造函數
????????public?DownFile()?
????????{
????????????newName?=?false;
????????????savePath?=?"c:\\";
????????}
????????#endregion
????????#region?私有方法
????????///?<summary>
????????///?隨機返回文件名
????????///?</summary>
????????///?<returns></returns>
????????private?string?getFileName()
????????{
????????????System.Random?objDom?=?new?Random();
????????????int?intNum?=?objDom.Next(1000,?9999);
????????????objDom?=?null;
????????????string?strNum?=?System.DateTime.Now.ToString("yyyyMMddhhmmss");
????????????return?strNum?+?intNum.ToString();
????????}
????????///?<summary>
????????///?根據文件得到文件擴展名
????????///?</summary>
????????///?<param?name="fileName">文件名</param>
????????///?<returns></returns>
????????private?string?getExtension(string?fileName)
????????{?
????????????int?start=fileName.IndexOf(".")+1;
????????????string?Ext?=?fileName.Substring(start,?fileName.Length?-?start);
????????????return?Ext;
????????}
????????#endregion
????????#region?公共方法
????????///?<summary>
????????///?保存遠程文件
????????///?</summary>
????????///?<param?name="filePath">遠程文件路徑</param>
????????///?<returns></returns>
????????public?string?SaveFile(string?filePath)
????????{
????????????string?fPath,?fName,?sPath;
????????????fPath?=?filePath.Replace("\\",?"/");
????????????int?start?=?fPath.LastIndexOf("/")?+?1;
????????????fName?=?fPath.Substring(start,?fPath.Length?-?start);
????????????if?(newName)
????????????{
????????????????sPath?=?savePath?+?getFileName()?+?"."?+?getExtension(fName);
????????????}
????????????else
????????????{
????????????????sPath?=?savePath?+?fName;
????????????}
????????????
????????????System.Net.WebClient?myWebClient?=?new?System.Net.WebClient();
????????????try
????????????{
????????????????myWebClient.DownloadFile(fPath,?sPath);
????????????}
????????????catch?(Exception?ee)
????????????{
????????????????throw?ee;
????????????}
????????????finally
????????????{
????????????????myWebClient.Dispose();
????????????}
????????????return?fName;
????????}
????????#endregion
????}
}
namespace?DownRemoteFile
{
????class?DownFile
????{
????????#region?成員變量
????????private?string?savePath;
????????private?bool?newName;
????????#endregion
????????#region?屬性
????????public?string?SavePath
????????{
????????????set?
????????????{?
????????????????savePath=value.Replace("/",?"\\");
????????????????if?(savePath.LastIndexOf("\\")?!=?savePath.Length?-?1)
????????????????????savePath?+=?"\\";
????????????}
????????????get
????????????{
????????????????return?savePath;
????????????}
????????}
????????public?bool?NewName
????????{
????????????set?{?newName?=?value;?}
????????????get?{?return?newName;?}
????????}
????????#endregion
????????#region?構造函數
????????public?DownFile()?
????????{
????????????newName?=?false;
????????????savePath?=?"c:\\";
????????}
????????#endregion
????????#region?私有方法
????????///?<summary>
????????///?隨機返回文件名
????????///?</summary>
????????///?<returns></returns>
????????private?string?getFileName()
????????{
????????????System.Random?objDom?=?new?Random();
????????????int?intNum?=?objDom.Next(1000,?9999);
????????????objDom?=?null;
????????????string?strNum?=?System.DateTime.Now.ToString("yyyyMMddhhmmss");
????????????return?strNum?+?intNum.ToString();
????????}
????????///?<summary>
????????///?根據文件得到文件擴展名
????????///?</summary>
????????///?<param?name="fileName">文件名</param>
????????///?<returns></returns>
????????private?string?getExtension(string?fileName)
????????{?
????????????int?start=fileName.IndexOf(".")+1;
????????????string?Ext?=?fileName.Substring(start,?fileName.Length?-?start);
????????????return?Ext;
????????}
????????#endregion
????????#region?公共方法
????????///?<summary>
????????///?保存遠程文件
????????///?</summary>
????????///?<param?name="filePath">遠程文件路徑</param>
????????///?<returns></returns>
????????public?string?SaveFile(string?filePath)
????????{
????????????string?fPath,?fName,?sPath;
????????????fPath?=?filePath.Replace("\\",?"/");
????????????int?start?=?fPath.LastIndexOf("/")?+?1;
????????????fName?=?fPath.Substring(start,?fPath.Length?-?start);
????????????if?(newName)
????????????{
????????????????sPath?=?savePath?+?getFileName()?+?"."?+?getExtension(fName);
????????????}
????????????else
????????????{
????????????????sPath?=?savePath?+?fName;
????????????}
????????????
????????????System.Net.WebClient?myWebClient?=?new?System.Net.WebClient();
????????????try
????????????{
????????????????myWebClient.DownloadFile(fPath,?sPath);
????????????}
????????????catch?(Exception?ee)
????????????{
????????????????throw?ee;
????????????}
????????????finally
????????????{
????????????????myWebClient.Dispose();
????????????}
????????????return?fName;
????????}
????????#endregion
????}
}
轉載于:https://www.cnblogs.com/hanwater/archive/2009/07/30/1535117.html
總結
以上是生活随笔為你收集整理的C#下载远程文件到本地的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 暹罗猫多少钱一只
- 下一篇: 谁看过女星王秋紫电影电视上作品?你看到替