以下代码是否正确将字节保存到文件

时间:2013-10-16 22:06:11

标签: c# c#-4.0

我正在尝试将字节数组保存到文件中。文件内容未保存到该文件。

HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create("imageurl");
String lsResponse = string.Empty;
using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse())
{
    using (BinaryReader reader = new BinaryReader(lxResponse.GetResponseStream()))
    {
        Byte[] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
        String path = Application.StartupPath + "\\abc.jpg";
        File.WriteAllBytes(path, lnByte);
    }
}

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)

using (Stream response = lxResponse.GetResponseStream())
using (FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
{
    response.CopyTo(fs);
}