Beispiel #1
0
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="downUrl">下载地址</param>
        /// <param name="downFile">要下载的文件</param>
        /// <returns></returns>
        public static bool DownFile(string downUrl, string downFile)
        {
            long fileLength = 0;

            WebRequest  webReq = WebRequest.Create(downUrl);
            WebResponse webRes = webReq.GetResponse();

            fileLength = webRes.ContentLength;
            try
            {
                Stream       srm        = webRes.GetResponseStream();
                StreamReader srmReader  = new StreamReader(srm);
                byte[]       bufferbyte = new byte[fileLength];
                int          allByte    = (int)bufferbyte.Length;
                int          startByte  = 0;
                while (fileLength > 0)
                {
                    int downByte = srm.Read(bufferbyte, startByte, allByte);
                    if (downByte == 0)
                    {
                        break;
                    }
                    startByte  += downByte;
                    fileLength -= downByte;
                }
                SysFile2.CreateDirtory(downFile);
                FileStream fs = new FileStream(downFile, FileMode.Create, FileAccess.Write);
                fs.Write(bufferbyte, 0, startByte);
                srm.Close();
                srmReader.Close();
                fs.Close();
                return(true);
            }
            catch
            {
                return(false);
            }
        }