Home > Net >  Not deserialization Stream through the binary file
Not deserialization Stream through the binary file

Time:09-22

Programming in the process of a series of pictures and let me give wrapped in a List, then stored for hard disk 123. TXT file, late to read convenient,
One, the original idea is to put this 123. TXT file to the user, when read true or move FileStream read,
After the test, the following code is available
 
String Spath=Environment. GetFolderPath (Environment) SpecialFolder) Desktop) + @ \ 123. TXT ";
FileStream stream=new FileStream (Spath, FileMode. Open);

BinaryFormatter bFormat=new BinaryFormatter ();
CardList=(List & gt;) BFormat. Deserialize (stream);//is an object deserialization. Have to do the type conversion
stream.Close();



Two, then I abrupt fantasy, why not put 123. TXT file is loaded into the kind of resources to do (class library right attributes - & gt; Resources - & gt; Add resources - & gt; Add an existing file, load 123. TXT)
Then through the Properties. The Resources. The ResourceManager. GetObject (" 123 "). The ToString (); Access to content, but after the test, the following code is not successful
 
String Str=Properties. The Resources. The ResourceManager. GetObject (" 123 "). The ToString ();
Byte [] array=Encoding. The ASCII. GetBytes (Str);
MemoryStream stream=new MemoryStream (array);

BinaryFormatter bFormat=new BinaryFormatter ();
CardList=(List & gt;) BFormat. Deserialize (stream);//is an object deserialization. Have to do the type conversion
stream.Close();


Wrong statements, error message: System. OutOfMemoryException: "the Exception of type 'System. OutOfMemoryException' was thrown."
How to change ah, excuse me,




In addition, I suddenly thought of, isn't me 123. TXT storage have been binary data, no longer need to Encoding. The ASCII. GetBytes (Str) turned binary, test the following usage,
 var Str=Properties. The Resources. The ResourceManager. GetObject (" CardPic "); 
Byte [] array=(byte []) Str;
MemoryStream stream=new MemoryStream (array);

BinaryFormatter bFormat=new BinaryFormatter ();
CardList=(List & gt;) BFormat. Deserialize (stream);//is an object deserialization. Have to do the type conversion
stream.Close();

No way!

CodePudding user response:

1. Since the save to file, why not just use image file?
2. The images can also be directly added to the resource
  •  Tags:  
  • C#
  • Related