如何使用智能设备应用程序下载.cab和.exe文件? [英] How to Download a .cab and .exe file using Smart Device Application?

查看:92
本文介绍了如何使用智能设备应用程序下载.cab和.exe文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpRequest.Credentials = CredentialCache.DefaultCredentials;
                
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

                System.IO.Stream dataStream = httpResponse.GetResponseStream();
                System.IO.StreamReader streamReader = new System.IO.StreamReader(dataStream);

                String data = streamReader.ReadToEnd();
                
                byte[] buffer = new byte[data.Length];

                for (int i = 0; i < data.Length; i++)
                    buffer[i] = (byte)data[i];
                
                dataStream.Close();
                streamReader.Close();
                
                FileStream fs = new FileStream("filename.txt", FileMode.CreateNew);
                BinaryWriter bw = new BinaryWriter(fs);

                for (int i = 0; i < buffer.Length; i++)
                    bw.Write(buffer[i]);

                streamReader.Close();
                httpResponse.Close();
            }
            catch (Exception pe)
            {
                System.Windows.Forms.MessageBox.Show(pe.Message);
            }


这是代码工作,当下载文本,word和excel文件时,如果我正在下载.cab或.exe文件,则表示它显示异常值超出范围"实际上我想下载.cab文件和.exe文件,如何下载那个???
谢谢


This is code is working, when downloading a text,word and excel files, if i am Downloading a .cab or .exe files means it shows an exception "Value out of range" Actually i want to download the .cab files and .exe files, How to Download That???
Thanks

推荐答案

您不能将二进制文件转换为字符串,然后再将其写入文件.

you cannot convert binary files to string and then write it to file.

using (FileStream destination = File.Open(@"some.exe",FileMode.CreateNew))
{
dataStream.CopyTo(destination);
}


这篇关于如何使用智能设备应用程序下载.cab和.exe文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆