0

我刚刚在 IIS 上部署了我的网站,在我的 WPF 端我想从中下载数据,所以我就是这样做的:

   public StartWindow()
    {
        InitializeComponent();
        label11.Width = Double.NaN;
        stackPanelTitle.Width = Double.NaN;
        DownloadData("http://localhost/iStellarMobile_deploy/Puzzle/CrossTest.txt"); // null exception 

    }

    protected void DownloadData(string strFileUrlToDownload)
    {
        byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception 

        MemoryStream storeStream = new MemoryStream();

        storeStream.SetLength(myDataBuffer.Length);
        storeStream.Write(myDataBuffer, 0, (int)storeStream.Length);

        storeStream.Flush();

        using (FileStream file = new FileStream(@"C:\TestFile.txt", FileMode.Create, System.IO.FileAccess.Write))
        {
            byte[] bytes = new byte[storeStream.Length];
            storeStream.Read(bytes, 0, (int)storeStream.Length);
            file.Write(bytes, 0, bytes.Length);
            storeStream.Close();
        }

        //TO save into certain file must exist on Local
       //storeStream.SaveAs(storeStream,  @"C:\TestFile.txt");

        //The below Getstring method to get data in raw format and manipulate it as per requirement
        string download = Encoding.ASCII.GetString(myDataBuffer);


    }

这是正确的方法吗?我指的是http://www.c-sharpcorner.com/UploadFile/97fc7a/reading-files-from-given-specific-url-using-webclient/

它在我评论的 2 行上给了我空异常。

4

1 回答 1

1

你为什么使用新的uri

 byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception

这是一个直接取自 msdn 的示例

WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);

http://msdn.microsoft.com/en-us/library/xz398a3f.aspx

于 2013-09-06T01:35:42.810 回答