0

由于我以前从未进行过序列化,因此在弄清楚如何执行此操作时遇到了一些麻烦。我能够将字符串转换为 byte[]s,然后将它们序列化为新字符串,但无法将该序列化字符串保存到文本文件中,重新导入并将其转换回原始值。请记住,这是一个学习项目。这就是我到目前为止所做的

private string SerializeToString(string obj)
    {
        if (obj == null)
            return null;

        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, obj);
        byte[] Array = ms.ToArray();
//This is where I create the txt file and write to it
            File.OpenWrite(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)     + "\\Text.txt")
            .Write(Array, 0, Array.Length);



        ms.Close();
        return Convert.ToBase64String(Array);
    }

        private void txtCheck_Click(object sender, RoutedEventArgs e)
    {
//I uploaded the file to drop box and can successfully download it but can not figure out
//how to get the original value out of it..
        string Url = "http://dl.dropbox.com/u/62170850/Text.txt";
        WebClient webClient = new WebClient();
        //webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
        //webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        byte[] myDataBuffer = webClient.DownloadData(new Uri(Url));
        string s = Convert.ToBase64String(myDataBuffer);
    }
4

1 回答 1

0

使用webClient.DownloadString并且不要忘记FromBase64String在反序列化之前通过调用将下载的字符串转换回字节数组。

于 2013-03-15T01:02:20.597 回答