std finally 設置 出錯 word div 獲取文件 mar soft

public class RemoteDownload
    {
        public static void DownLoad(string addressUrl,string localName)
        {
            //下載文件
            System.NET.WebClient myWebClient = new System.Net.WebClient();
            myWebClient.DownloadFile(@"/10.2.0.254/software/01279.lic.txt", "testdownload.txt");           
            //下載end
        }
    }
 通過URL獲取頁面內容
   try
            {
                // 遠程獲取目標頁面源碼
                string strTargetHtml = string.Empty;
                WebClient wc = new WebClient();
                wc.Credentials = CredentialCache.DefaultCredentials;
                byte[] btPageData = wc.DownloadData(strTargetUrl + dtTargetDate.ToString("yyyy") + "/" + dtTargetDate.ToString("MM") + "/" + dtTargetDate.ToString("dd") + "/");
                strTargetHtml = Encoding.UTF8.GetString(btPageData);
                wc.Dispose();
            }
            catch(Exception exp)
            {
                _isError = true;
                _errorDetail = "獲取目標日誌文件列表時出錯:" + exp.Message;
            }
    
通過web方式,從遠程服務器端下載文件:
 
public class WebDownload
    {
        public static void DownLoad(string Url, string FileName)
        {
            bool Value = false;
            WebResponse response = null;
            Stream stream = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                response = request.GetResponse();
                stream = response.GetResponseStream();
                if (!response.ContentType.ToLower().StartsWith("text/"))
                {
                    Value = SaveBinaryFile(response, FileName);
                }
            }
            catch (Exception err)
            {
                string aa = err.ToString();
            }
        }
        /// <summary>
        /// Save a binary file to disk.
        /// </summary>
        /// <param name="response">The response used to save the file</param>
        // 將二進制文件保存到磁盤
        private static bool SaveBinaryFile(WebResponse response, string FileName)
        {
            bool Value = true;
            byte[] buffer = new byte[1024];
            try
            {
                if (File.Exists(FileName))
                    File.Delete(FileName);
                Stream outStream = System.IO.File.Create(FileName);
                Stream inStream = response.GetResponseStream();
                int l;
                do
                {
                    l = inStream.Read(buffer, 0, buffer.Length);
                    if (l > 0)
                        outStream.Write(buffer, 0, l);
                }
                while (l > 0);
                outStream.Close();
                inStream.Close();
            }
            catch
            {
                Value = false;
            }
            return Value;
        }
 
從FTP上下載文件:
 
 public class FtpDownload
    {
        public static void DownLoad(string FtpPath)
        {
            /*首先從配置文件讀取ftp的登錄信息*/
            string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString();
            string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName"].ToString();
            string FtpPassWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();
            string LocalFileExistsOperation = System.Configuration.ConfigurationManager.AppSettings["LocalFileExistsOperation"].ToString();

            Uri uri = new Uri(FtpPath);
            string FileName = Path.GetFullPath(TempFolderPath) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath);
            //創建一個文件流
            FileStream fs = null;
            Stream responseStream = null;
            try
            {
                //創建一個與FTP服務器聯系的FtpWebRequest對象
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                //設置請求的方法是FTP文件下載
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                //連接登錄FTP服務器
                request.Credentials = new NetworkCredential(FtpUserName, FtpPassWord);
                //獲取一個請求響應對象
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                //獲取請求的響應流
                responseStream = response.GetResponseStream();
                //判斷本地文件是否存在,如果存在,則打開和重寫本地文件
                if (File.Exists(FileName))
                {
                    if (LocalFileExistsOperation == "write")
                    {
                        fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);
                    }
                }
                //判斷本地文件是否存在,如果不存在,則創建本地文件
                else
                {
                    fs = File.Create(FileName);
                }
                if (fs != null)
                {
                    int buffer_count = 65536;
                    byte[] buffer = new byte[buffer_count];
                    int size = 0;
                    while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
                    {
                        fs.Write(buffer, 0, size);
                    }
                    fs.Flush();
                    fs.Close();
                    responseStream.Close();
                }
            }
            finally
            {
                if (fs != null)
                    fs.Close();
                if (responseStream != null)
                    responseStream.Close();
            }

        }
    }
.

C# 獲取文件下載的各種方法