c# Serialize 存取 Class
//c# Serialize的簡單範例 & binary和xml存檔方式比較 http://blog.wahahajk.com/2009/06/c-serialize-binaryxml.html
[Serializable]
public class My_sys_ini
{
public string text="";
public int Com_i=0;
}
string sys_ini_file = Application.StartupPath + @"\Myini.s";
private void Read_ini(string F_name, int wr_type = 0)
{
My_sys_ini myobj=new My_sys_ini();
if(!File.Exists(F_name))
{
myobj.text=Application.StartupPath;
Write_ini(myobj, F_name, wr_type);
}
if (wr_type == 0) //binary讀檔
{
IFormatter binFmt = new BinaryFormatter();
Stream s = File.Open(sys_ini_file , FileMode.Open);
myobj = (My_sys_ini)binFmt.Deserialize(s);
s.Close();
}
else //xml讀檔
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(myobj.GetType());
Stream sx = File.Open(sys_ini_file , FileMode.Open);
myobj = (My_sys_ini)ser.Deserialize(sx);
sx.Close();
}
_TSTextBox_Dir.Text = myobj.text;
SYS_Save_Path = myobj.text;
_TSComboBox1.SelectedIndex = myobj.Com_i;
}
private void Write_ini(My_sys_ini myobj, string F_name, int wr_type = 0)
{
if (wr_type == 0) //binary存檔
{
IFormatter binFmt = new BinaryFormatter();
Stream s = File.Open(F_name, FileMode.Create);
binFmt.Serialize(s, myobj);
s.Close();
}
else //xml存檔
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(myobj.GetType());
Stream sx = File.Open(F_name, FileMode.Create);
ser.Serialize(sx, myobj);
sx.Close();
}
}
[Serializable]
public class My_sys_ini
{
public string text="";
public int Com_i=0;
}
string sys_ini_file = Application.StartupPath + @"\Myini.s";
private void Read_ini(string F_name, int wr_type = 0)
{
My_sys_ini myobj=new My_sys_ini();
if(!File.Exists(F_name))
{
myobj.text=Application.StartupPath;
Write_ini(myobj, F_name, wr_type);
}
if (wr_type == 0) //binary讀檔
{
IFormatter binFmt = new BinaryFormatter();
Stream s = File.Open(sys_ini_file , FileMode.Open);
myobj = (My_sys_ini)binFmt.Deserialize(s);
s.Close();
}
else //xml讀檔
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(myobj.GetType());
Stream sx = File.Open(sys_ini_file , FileMode.Open);
myobj = (My_sys_ini)ser.Deserialize(sx);
sx.Close();
}
_TSTextBox_Dir.Text = myobj.text;
SYS_Save_Path = myobj.text;
_TSComboBox1.SelectedIndex = myobj.Com_i;
}
private void Write_ini(My_sys_ini myobj, string F_name, int wr_type = 0)
{
if (wr_type == 0) //binary存檔
{
IFormatter binFmt = new BinaryFormatter();
Stream s = File.Open(F_name, FileMode.Create);
binFmt.Serialize(s, myobj);
s.Close();
}
else //xml存檔
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(myobj.GetType());
Stream sx = File.Open(F_name, FileMode.Create);
ser.Serialize(sx, myobj);
sx.Close();
}
}
留言
張貼留言