• C#下载文件


    /// <summary>        
            
    /// 下载文件        
            
    /// </summary>        
            
    /// <param name="URL">下载文件地址</param>       
            
    /// 
            
    /// <param name="Filename">下载后的存放地址</param>        
            
    /// <param name="Prog">用于显示的进度条</param>        
            
    /// 
            public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
            {
                
    try
                {
                    System.Net.HttpWebRequest Myrq 
    = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                    System.Net.HttpWebResponse myrp 
    = (System.Net.HttpWebResponse)Myrq.GetResponse();
                    
    long totalBytes = myrp.ContentLength;
                    
    if (prog != null)
                    {
                        prog.Maximum 
    = (int)totalBytes;
                    }
                    System.IO.Stream st 
    = myrp.GetResponseStream();
                    System.IO.Stream so 
    = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                    
    long totalDownloadedByte = 0;
                    
    byte[] by = new byte[1024];
                    
    int osize = st.Read(by, 0, (int)by.Length);
                    
    while (osize > 0)
                    {
                        totalDownloadedByte 
    = osize + totalDownloadedByte;
                        System.Windows.Forms.Application.DoEvents();
                        so.Write(by, 
    0, osize);
                        
    if (prog != null)
                        {
                            prog.Value 
    = (int)totalDownloadedByte;
                        }
                        osize 
    = st.Read(by, 0, (int)by.Length);
                    }
                    so.Close();
                    st.Close();
                }
                
    catch (System.Exception)
                {
                    
    throw;
                }
            }
  • 相关阅读:
    Dumpbin 工具的使用
    ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
    directdraw显示yuv视频,出现屏保时,yuv显示不出来,表面丢失
    DirectX截图黑屏的解决办法
    VS2008 Project : error PRJ0019: 某个工具从以下位置返回了错误代码: "正在执行生成后事件..."解决方案
    RoundingMode 几个参数详解
    IDEA导入eclipse项目并部署运行完整步骤(转发)
    Intellij idea操作maven时控制台中文乱码
    java 替换json字符串中间的引号保留两边的引号,避免json校验失败
    分布式ID解决方案
  • 原文地址:https://www.cnblogs.com/yangxiaohu1/p/1373048.html
Copyright © 2020-2023  润新知