xml数据库自动保存全部控件 datagridview的数据,picturebox里面的image图片,,,,textbox,numericalupdown,checkebox的内容,包括容器panel,groupbox,tabcontrol内的控件
xml数据库自动保存全部控件picturebox 的image图片,,, 通过把image转为byte【】 ,再转string 就可以储存在string【】 里面的了。
datagridview也是row col的, ;来分行和列的。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Windows.Forms ;
using System.Drawing;
namespace save_all
{
public class config
{
// public Queue <data_xml > data_Xmls=new Queue <data_xml >();
public string[] name = new string[100];//wenqizhivolt
public string[] value = new string[100];//wenqizhivolt
}
static class wen
{
public static config Config = new config();
}
public static class readWriteConfigParam
{
public static void Save(Control.ControlCollection ct)
{
save_controls(ct);
string strFilePath = "data.xml";
XmlSerializer serializer = new XmlSerializer(typeof(config));
FileStream fs;
fs = new FileStream(strFilePath, FileMode.Create);
serializer.Serialize(fs, wen.Config);
fs.Close();
}
public static void Load(RichTextBox richTextBox, Control.ControlCollection ct)
{
if (ct==null )
{
return;
}
string strFilePath = "data.xml";
if (!File.Exists(strFilePath))
{
if (richTextBox != null)
{
richTextBox.AppendText(strFilePath + "打开失败,没有文件\r\n");
}
return;
}
XmlSerializer serializer = new XmlSerializer(typeof(config));
FileStream fs;
fs = new FileStream(strFilePath, FileMode.Open);
wen.Config = (config)serializer.Deserialize(fs);
fs.Close();
show_config_to_ct(ct);
if (richTextBox != null)
{
richTextBox.AppendText(strFilePath + "打开成功,参数文件读取到\r\n");
}
}
public static void save_ct_to_config(Control.ControlCollection ct,ref int index)
{
foreach (Control item in ct)
{
if (item is GroupBox)
{
save_ct_to_config(item.Controls,ref index);
}
if (item is Panel)
{
save_ct_to_config(item.Controls, ref index);
}
if (item is TabControl)
{
save_ct_to_config(item.Controls, ref index);
}
if (item is CheckBox)
{
CheckBox c = item as CheckBox;
fillTable(c.Name, c.Checked.ToString(),ref index);
}
if (item is TextBox)
{
fillTable(item.Name, item.Text,ref index);
}
//if (item is Label)
//{
// fillTable(item.Name, item.Text,ref index);
//}
if (item is NumericUpDown)
{
NumericUpDown c = item as NumericUpDown;
fillTable(c.Name, c.Value.ToString(), ref index);
}
if (item is PictureBox )
{
PictureBox p = item as PictureBox;
string s = get_string_by_image(p.Image );
fillTable(p.Name, s, ref index);
}
if (item is DataGridView )
{
DataGridView dataGridView = item as DataGridView;
fillTable(dataGridView.Name, data_to_string (dataGridView ), ref index);
}
}
} //插入一些数据
static void save_controls(Control.ControlCollection ct)
{
int i = 0;
save_ct_to_config(ct,ref i);
}
// static int index = 0;
public static void fillTable(string name, string value,ref int i)
{
if (name ==null )
{
return;
}
if (value ==null )
{
return;
}
wen.Config.name[i] = name;
wen.Config.value[i] = value;
if (i < wen.Config.name.Length - 1)
{
i++;
}
}
static string get_value_by_name(string n)
{
if (n=="")return null ;
for (int i = 0; i < wen.Config.name.Length; i++)
{
string nn = wen.Config.name[i];
if (nn == "")
{
return null ;
}
if (n == nn)
{
return wen.Config.value[i];
}
}
return null ;
}
public static void show_config_to_ct(Control.ControlCollection ct)
{
foreach (Control item in ct)
{
if (item is GroupBox)
{
show_config_to_ct(item.Controls);
}
if (item is Panel)
{
show_config_to_ct(item.Controls);
}
if (item is TabControl)
{
show_config_to_ct(item.Controls);
}
if (item is TextBox)
{
string s = get_value_by_name(item.Name);
if (s!=null )
{
item.Text = s;
}
}
//if (item is Label)
//{
// item.Text = get_value_by_name(item.Name);
//}
if (item is CheckBox)
{
CheckBox tx = item as CheckBox;
string value = get_value_by_name(item.Name);
if (value != null )
{
try
{
bool b = Convert.ToBoolean(value);
tx.Checked = b;
}
catch (Exception)
{
}
}
}
if (item is NumericUpDown)
{
NumericUpDown tx = item as NumericUpDown;
string value = get_value_by_name(item.Name);
if (value != null)
{
try
{
decimal d = Convert.ToDecimal(value);
tx.Value = d;
}
catch (Exception)
{
}
}
}
if (item is PictureBox )
{
PictureBox p = item as PictureBox;
string value = get_value_by_name(item.Name);
if (value != null )
{
try
{
Image image = get_image_by_string(value );
p.Image = image;
}
catch (Exception)
{
}
}
}
if (item is DataGridView )
{
DataGridView dataGridView = item as DataGridView;
string value = get_value_by_name(item.Name);
if (value != null)
{
string_to_data(value ,ref dataGridView );
try
{ }
catch (Exception)
{
}
}
}
}
}
public static string data_to_string(DataGridView dataGridView)
{
int x = dataGridView.Rows.Count;
int y = dataGridView.Columns.Count;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < x ; i++)
{
for (int j = 0; j < y ; j++)
{
string s = "";
if (dataGridView.Rows[i].Cells[j].Value != null)
{
s = dataGridView.Rows[i].Cells[j].Value.ToString();
}
stringBuilder.Append(s+",");
}
stringBuilder.Append(";");
}
return stringBuilder.ToString();
}
public static void string_to_data(string ss, ref DataGridView dataGridView)
{
//int x = dataGridView.Rows.Count;
//int y = dataGridView.Columns.Count;
string[] splite_fen = ss.Split(';');
int x = splite_fen.Length;
int x_rows =x- dataGridView.Rows.Count;
if (x_rows > 0)
{
for (int i = 0; i < x_rows; i++)
{
dataGridView.Rows.Add();
}
}
for (int i = 0; i < x; i++)
{
string[] splite_dou = splite_fen[i].Split(',');
for (int j = 0; j < splite_dou.Length; j++)
{
string s = splite_dou[j];
if (j < dataGridView.Columns.Count)
{
dataGridView.Rows[i].Cells[j].Value = s;
}
}
}
}
public static Image GetImageByBytes(byte[] bytes)
{
Image photo = null;
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Write(bytes, 0, bytes.Length);
photo = Image.FromStream(ms, true);
}
return photo;
}
public static byte[] GetByteImage(Image img)
{
byte[] bt = null;
if (!img.Equals(null))
{
using (MemoryStream mostream = new MemoryStream())
{
Bitmap bmp = new Bitmap(img);
bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Bmp);//将图像以指定的格式存入缓存内存流
bt = new byte[mostream.Length];
mostream.Position = 0;//设置留的初始位置
mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
}
}
return bt;
}
public static byte[] stringtobyte(string str)
{
byte[] decBytes = Convert.FromBase64String(str);
return decBytes;
}
public static string bytetostring(byte[] bytes)
{
string str = Convert.ToBase64String(bytes);
return str;
}
public static Image get_image_by_string(string s)
{
if (s==null )
{
return null;
}
if (s.Length ==0)
{
return null ;
}
Image image = GetImageByBytes(stringtobyte(s));
return image;
}
public static string get_string_by_image(Image image)
{
if (image == null)
{
return null;
}
return bytetostring(GetByteImage(image));
}
}
}