C#文件下载(断点续传)----运行错误:转换到COFF期间失败

代码转载自http://blog.csdn.net/haoxiang110/article/details/51556099,如有侵权,请联系删除

实验的时候本来想本机搭个HTTP服务器,但在公司电脑不便操作,就找个百度里面下载压缩wrar软件的路径

class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = "http://sw.bos.baidu.com/sw-search-sp/software/fe6cf004948ba/wrar_5.40.0.0_scp.exe";
            string destFile = @"D:\";
            WebDown.DownloadFile(sourceFile, destFile);
            Console.ReadKey();
        }
    }

    public class WebDown
    {
        public static bool DownloadFile(string sourceFile, string destFile)
        {
            bool flag = false;
            long sPosition = 0;
            FileStream fStream = null;
            Stream mStream = null;

            string fileName = sourceFile.Substring(sourceFile.LastIndexOf(@"/") + 1);
            if (destFile.EndsWith("\\"))
            {
                destFile = destFile + fileName;
            }
            else
            {
                destFile = destFile + "\\" + fileName;
            }

            try
            {
                if (File.Exists(destFile))
                {
                    fStream = File.OpenWrite(destFile);
                    sPosition = fStream.Length;
                    long httpLen = GetHttpLength(sourceFile);
                    if (sPosition == httpLen)
                    {
                        Console.WriteLine("Already done!");
                        return true;
                    }
                    fStream.Seek(sPosition, SeekOrigin.Current);
                }
                else
                {
                    fStream = File.Create(destFile);
                }

                HttpWebRequest mRequest = (HttpWebRequest)HttpWebRequest.Create(sourceFile);
                if (sPosition > 0)
                {
                    mRequest.AddRange((int)sPosition);
                }
                mStream = mRequest.GetResponse().GetResponseStream();
                byte[] btContent = new byte[512];
                int size = 0;
                size = mStream.Read(btContent, 0, 512);
                while (size > 0)
                {
                    fStream.Write(btContent, 0, size);
                    size = mStream.Read(btContent, 0, 512);
                }
                Console.WriteLine("Download complete!");
                flag = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if (fStream != null)
                {
                    fStream.Close();
                }
                if (mStream != null)
                {
                    mStream.Close();
                }
            }

            return flag;
        }

        public static long GetHttpLength(string sourceFile)
        {
            long length = 0;

            try
            {
                var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(sourceFile));
                req.Method = "HEAD";
                req.Timeout = 5000;
                var res = (HttpWebResponse)req.GetResponse();
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    length = res.ContentLength;
                }
                res.Close();
            }
            catch
            {
                length = 0;
            }

            return length;
        }
    }
实验的时候发现一个运行错误:

LINK:fatal error LNK1123:转换到COFF期间失败:文件无效或损坏

网上查找解决方案,贴上来备用

VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 “是”改为“否”即可,但是没新建一个项目都要这样设置一次。

在建立VS2010 Win32 Project项目时,按照上面解决方案依然发生了“error LNK1123”错误,经过上网查资料,解决方案为:

第一步:与上相同
第二步:将 项目|项目属性|配置属性|连接器|清单文件|嵌入清单 “是”改为“否”
第三步:一般计算机经过上两步设置就能解决问题了,但是如果还有问题,那就按一下方法解决
计算机是否为64bit操作系统,查找是否有两个cvtres.exe
一个是C:\Program Files(x86)\Microsoft Visual Studio 10.0\vc\bin\cvtres.exe
另一个是D:\ProgramFiles (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\cvtres.exe
右键属性|详细信息 查看两者版本号,删除/重命名较旧的版本,或者重新设置Path变量
意外的是,治本的办法是第三步,删除旧版本的cvtres.exe后,就不需要每次都设置配置了。
还有一种方法就是安装新的VS2010 SP1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值