单击按钮时将数据保存到文件中?

时间:2013-01-09 08:46:42

标签: c# winforms

我正在C#中为一个ledcube申请。 它现在所做的就是允许用户点击某些按钮来改变所需LED的颜色。 我试图暗示一个允许用户将当前配置(颜色)保存到文件的功能,以后可以通过编程bord读取。 保存的文件必须包含每个led的二进制代码(LED是否打开,以及哪种颜色:关闭,红色,绿色,橙色)。 我以为1 led可以包含00到11之间的值(所以0到3)。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    Button[,] laag_1 = new Button[8, 8];
    Button[,] laag_2 = new Button[8, 8];
    Button[,] laag_3 = new Button[8, 8];
    Button[,] laag_4 = new Button[8, 8];
    Button[,] laag_5 = new Button[8, 8];
    Button[,] laag_6 = new Button[8, 8];
    Button[,] laag_7 = new Button[8, 8];
    Button[,] laag_8 = new Button[8, 8];

    public Form1()
    {
        InitializeComponent();
        for (int x = 0; x < laag_1.GetLength(0); x++)
        {
            for (int y = 0; y < laag_1.GetLength(1); y++)
            {
                laag_1[x, y] = new Button();
                laag_1[x, y].SetBounds((50 * x) + 100, (50 * y) + 30, 40, 40);
                laag_1[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_1[x, y]);
                laag_1[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_2.GetLength(0); x++)
        {
            for (int y = 0; y < laag_1.GetLength(1); y++)
            {
                laag_2[x, y] = new Button();
                laag_2[x, y].SetBounds((50 * x) + 520, (50 * y) + 30, 40, 40);
                laag_2[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_2[x, y]);
                laag_2[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_3.GetLength(0); x++)
        {
            for (int y = 0; y < laag_3.GetLength(1); y++)
            {
                laag_3[x, y] = new Button();
                laag_3[x, y].SetBounds((50 * x) + 940, (50 * y) + 30, 40, 40);
                laag_3[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_3[x, y]);
                laag_3[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_4.GetLength(0); x++)
        {
            for (int y = 0; y < laag_4.GetLength(1); y++)
            {
                laag_4[x, y] = new Button();
                laag_4[x, y].SetBounds((50 * x) + 1360, (50 * y) + 30, 40, 40);
                laag_4[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_4[x, y]);
                laag_4[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_5.GetLength(0); x++)
        {
            for (int y = 0; y < laag_5.GetLength(1); y++)
            {
                laag_5[x, y] = new Button();
                laag_5[x, y].SetBounds((50 * x) + 100, (50 * y) + 520, 40, 40);
                laag_5[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_5[x, y]);
                laag_5[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_6.GetLength(0); x++)
        {
            for (int y = 0; y < laag_6.GetLength(1); y++)
            {
                laag_6[x, y] = new Button();
                laag_6[x, y].SetBounds((50 * x) + 520, (50 * y) + 520, 40, 40);
                laag_6[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_6[x, y]);
                laag_6[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_7.GetLength(0); x++)
        {
            for (int y = 0; y < laag_7.GetLength(1); y++)
            {
                laag_7[x, y] = new Button();
                laag_7[x, y].SetBounds((50 * x) + 940, (50 * y) + 520, 40, 40);
                laag_7[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_7[x, y]);
                laag_7[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_8.GetLength(0); x++)
        {
            for (int y = 0; y < laag_8.GetLength(1); y++)
            {
                laag_8[x, y] = new Button();
                laag_8[x, y].SetBounds((50 * x) + 1360, (50 * y) + 520, 40, 40);
                laag_8[x, y].Click += new EventHandler(this.btnEvent_click);
                Controls.Add(laag_8[x, y]);
                laag_8[x, y].BackColor = Color.Black;
            }
        }
        this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
        LoadFromFile();
    }

    void btnEvent_click(object sender, EventArgs e)
    {
        Control ctrl = ((Control)sender);
        switch (ctrl.BackColor.Name)
        {
            case "Red":
                ctrl.BackColor = Color.Green;
                break;
            case "Black":
                ctrl.BackColor = Color.Red;
                break;
            case "Green":
                ctrl.BackColor = Color.Yellow;
                break;
            case "Yellow":
                ctrl.BackColor = Color.Black;
                break;
            default:
                ctrl.BackColor = Color.Black;
                break;
        }
    }

    void SaveEventHandler(object sender, EventArgs e)
    {
        SaveToFile();
    }

    private const string filePath = @"C:\testmap\laag_1.txt";
    private void LoadFromFile()
    {
        if (!System.IO.File.Exists(filePath))
            return;

        byte[] data = System.IO.File.ReadAllBytes(filePath);
        if (data == null || data.Length != laag_1.GetLength(0) * laag_1.GetLength(1) * 2)
            return;

        for (int x = 0; x < laag_1.GetLength(0); x++)
        {
            for (int y = 0; y < laag_1.GetLength(1); y++)
            {
                int position = (y * laag_1.GetLength(0) + x);

                string value = ((char)data[2 * position]).ToString() + ((char)data[2 * position + 1]).ToString();
                Color color;
                switch (value)
                {
                    case "01":
                        color = Color.Red;
                        break;
                    case "00":
                        color = Color.Black;
                        break;
                    case "10":
                        color = Color.Green;
                        break;
                    case "11":
                        color = Color.Yellow;
                        break;
                    default:
                        color = Color.Black;
                        break;
                }

                laag_1[x, y].BackColor = color;
            }
        }

    }

    private void SaveToFile()
    {
        Dictionary<Form1, int> d = new Dictionary<Form1, int>();

        byte[] data = new byte[laag_1.GetLength(0) * laag_1.GetLength(1) * 2];
        for (int x = 0; x < laag_1.GetLength(0); x++)
        {
            for (int y = 0; y < laag_1.GetLength(1); y++)
            {
                int position = (y * laag_1.GetLength(0) + x);
                string value;
                switch (laag_1[x, y].BackColor.Name)
                {
                    case "Red":
                        value = "01";
                        break;
                    case "Black":
                        value = "00";
                        break;
                    case "Green":
                        value = "10";
                        break;
                    case "Yellow":
                        value = "11";
                        break;
                    default:
                        value = "00";
                        break;
                }
                data[2 * position] = (byte)value[0];
                data[2 * position + 1] = (byte)value[1];
            }
        }

        System.IO.File.WriteAllBytes(filePath, data);
    }



    private void button1_Click(object sender, EventArgs e)
    {
        for (int x = 0; x < laag_1.GetLength(0); x++)
        {
            for (int y = 0; y < laag_1.GetLength(1); y++)
            {
                laag_1[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_2.GetLength(0); x++)
        {
            for (int y = 0; y < laag_2.GetLength(1); y++)
            {
                laag_2[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_3.GetLength(0); x++)
        {
            for (int y = 0; y < laag_3.GetLength(1); y++)
            {
                laag_3[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_4.GetLength(0); x++)
        {
            for (int y = 0; y < laag_4.GetLength(1); y++)
            {
                laag_4[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_5.GetLength(0); x++)
        {
            for (int y = 0; y < laag_5.GetLength(1); y++)
            {
                laag_5[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_6.GetLength(0); x++)
        {
            for (int y = 0; y < laag_6.GetLength(1); y++)
            {
                laag_6[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_7.GetLength(0); x++)
        {
            for (int y = 0; y < laag_7.GetLength(1); y++)
            {
                laag_7[x, y].BackColor = Color.Black;
            }
        }
        for (int x = 0; x < laag_8.GetLength(0); x++)
        {
            for (int y = 0; y < laag_8.GetLength(1); y++)
            {
                laag_8[x, y].BackColor = Color.Black;
            }
        }
    }

 }
}

3 个答案:

答案 0 :(得分:1)

您可以查看保存和加载文件的File.WriteAllBytesFile.ReadAllBytes方法。

另请查看OpenFileDialog控件,以选择要打开的文件。

此外,没有必要对值进行吝啬,你可以(并且应该)为每个按钮使用一个完整的字节,只需循环按钮并将值插入byte [],然后将它们写入文件

答案 1 :(得分:1)

我假设你想为此使用对象序列化

XMLSerialization http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization BinarySerialization http://msdn.microsoft.com/en-us/library/72hyey7b(v=vs.71).aspx

如果您想保存数据然后由其他应用程序读取,我建议您进行序列化,因为您不必担心从文件中读取数据。

答案 2 :(得分:1)

试试这个:

private const string filePath = @"d:\test.txt";
private void LoadFromFile()
{
    byte[] data = System.IO.File.ReadAllBytes(filePath);
    for (int x = 0; x < btn.GetLength(0); x++)
    {
        for (int y = 0; y < btn.GetLength(1); y++)
        {
            int position = (x * btn.GetLength(1) + y) * 2;
            int index = position / 8;
            int shift = position % 8;

            byte value = (byte)((data[index] >> shift) % 4);
            Color color;
            switch (value)
            {
                case 1:
                    color = Color.Red;
                    break;
                case 0:
                    color = Color.Black;
                    break;
                case 2:
                    color = Color.Green;
                    break;
                case 3:
                    color = Color.Yellow;
                    break;
                default:
                    color = Color.Black;
                    break;
            }

            btn[x, y].BackColor = color;
        }
    }
}

private void SaveToFile()
{
    byte[] data = new byte[(7 + btn.GetLength(0) * btn.GetLength(1) * 2) / 8]; 
    for (int x = 0; x < btn.GetLength(0); x++)
    {
        for (int y = 0; y < btn.GetLength(1); y++)
        {
            int position = (x * btn.GetLength(1) + y) * 2;
            byte value;
            switch (btn[x, y].BackColor.Name)
            {
                case "Red":
                    value = 1;
                    break;
                case "Black":
                    value = 0;
                    break;
                case "Green":
                    value = 2;
                    break;
                case "Yellow":
                    value = 3;
                    break;
                default:
                    value = 0;
                    break;
            }
            int index = position / 8;
            int shift = position % 8;
            data[index] = (byte)(data[index] | (value << shift));
        }
    }

    System.IO.File.WriteAllBytes(filePath, data);
}

[更新]:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button[,] btn = new Button[8, 8];
        public Form1()
        {
            InitializeComponent();
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    btn[x, y] = new Button();
                    btn[x, y].SetBounds((50 * x) + 30, (50 * y) + 30, 40, 40);
                    btn[x, y].Click += new EventHandler(this.btnEvent_click);
                    Controls.Add(btn[x, y]);
                    btn[x, y].BackColor = Color.Black;
                }
            }

            this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
            if (System.IO.File.Exists(filePath))
                LoadFromFile();
        }

        void btnEvent_click(object sender, EventArgs e)
        {
            Control ctrl = ((Control)sender);
            switch (ctrl.BackColor.Name)
            {
                case "Red":
                    ctrl.BackColor = Color.Green;
                    break;
                case "Black":
                    ctrl.BackColor = Color.Red;
                    break;
                case "Green":
                    ctrl.BackColor = Color.Yellow;
                    break;
                case "Yellow":
                    ctrl.BackColor = Color.Black;
                    break;
                default:
                    ctrl.BackColor = Color.Black;
                    break;
            }
        }

        void SaveEventHandler(object sender, EventArgs e)
        {
            SaveToFile();
        }

        private const string filePath = @"d:\test.txt";
        private void LoadFromFile()
        {
            byte[] data = System.IO.File.ReadAllBytes(filePath);
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (x * btn.GetLength(1) + y) * 2;
                    int index = position / 8;
                    int shift = position % 8;

                    byte value = (byte)((data[index] >> shift) % 4);
                    Color color;
                    switch (value)
                    {
                        case 1:
                            color = Color.Red;
                            break;
                        case 0:
                            color = Color.Black;
                            break;
                        case 2:
                            color = Color.Green;
                            break;
                        case 3:
                            color = Color.Yellow;
                            break;
                        default:
                            color = Color.Black;
                            break;
                    }

                    btn[x, y].BackColor = color;
                }
            }
        }

        private void SaveToFile()
        {
            byte[] data = new byte[(7 + btn.GetLength(0) * btn.GetLength(1) * 2) / 8]; 
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (x * btn.GetLength(1) + y) * 2;
                    byte value;
                    switch (btn[x, y].BackColor.Name)
                    {
                        case "Red":
                            value = 1;
                            break;
                        case "Black":
                            value = 0;
                            break;
                        case "Green":
                            value = 2;
                            break;
                        case "Yellow":
                            value = 3;
                            break;
                        default:
                            value = 0;
                            break;
                    }
                    int index = position / 8;
                    int shift = position % 8;
                    data[index] = (byte)(data[index] | (value << shift));
                }
            }

            System.IO.File.WriteAllBytes(filePath, data);
        }
    }
}

[更新#2]将存储重新编程为每个led的完整字节:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button[,] btn = new Button[8, 8];
        public Form1()
        {
            InitializeComponent();
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    btn[x, y] = new Button();
                    btn[x, y].SetBounds((50 * x) + 30, (50 * y) + 30, 40, 40);
                    btn[x, y].Click += new EventHandler(this.btnEvent_click);
                    Controls.Add(btn[x, y]);
                    btn[x, y].BackColor = Color.Black;
                }
            }

            this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
            LoadFromFile();
        }

        void btnEvent_click(object sender, EventArgs e)
        {
            Control ctrl = ((Control)sender);
            switch (ctrl.BackColor.Name)
            {
                case "Red":
                    ctrl.BackColor = Color.Green;
                    break;
                case "Black":
                    ctrl.BackColor = Color.Red;
                    break;
                case "Green":
                    ctrl.BackColor = Color.Yellow;
                    break;
                case "Yellow":
                    ctrl.BackColor = Color.Black;
                    break;
                default:
                    ctrl.BackColor = Color.Black;
                    break;
            }
        }

        void SaveEventHandler(object sender, EventArgs e)
        {
            SaveToFile();
        }

        private const string filePath = @"d:\test.txt";
        private void LoadFromFile()
        {
            if (!System.IO.File.Exists(filePath))
                return;

            byte[] data = System.IO.File.ReadAllBytes(filePath);
            if (data == null || data.Length != btn.GetLength(0) * btn.GetLength(1))
                return;

            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);

                    char value = (char)data[position];
                    Color color;
                    switch (value)
                    {
                        case '1':
                            color = Color.Red;
                            break;
                        case '0':
                            color = Color.Black;
                            break;
                        case '2':
                            color = Color.Green;
                            break;
                        case '3':
                            color = Color.Yellow;
                            break;
                        default:
                            color = Color.Black;
                            break;
                    }

                    btn[x, y].BackColor = color;
                }
            }
        }

        private void SaveToFile()
        {
            byte[] data = new byte[btn.GetLength(0) * btn.GetLength(1)]; 
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);
                    char value;
                    switch (btn[x, y].BackColor.Name)
                    {
                        case "Red":
                            value = '1';
                            break;
                        case "Black":
                            value = '0';
                            break;
                        case "Green":
                            value = '2';
                            break;
                        case "Yellow":
                            value = '3';
                            break;
                        default:
                            value = '0';
                            break;
                    }
                    data[position] = (byte)value;
                }
            }

            System.IO.File.WriteAllBytes(filePath, data);
        }
    }
}

更新#3:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button[,] btn = new Button[8, 8];
        public Form1()
        {
            InitializeComponent();
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    btn[x, y] = new Button();
                    btn[x, y].SetBounds((50 * x) + 30, (50 * y) + 30, 40, 40);
                    btn[x, y].Click += new EventHandler(this.btnEvent_click);
                    Controls.Add(btn[x, y]);
                    btn[x, y].BackColor = Color.Black;
                }
            }

            this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
            LoadFromFile();
        }

        void btnEvent_click(object sender, EventArgs e)
        {
            Control ctrl = ((Control)sender);
            switch (ctrl.BackColor.Name)
            {
                case "Red":
                    ctrl.BackColor = Color.Green;
                    break;
                case "Black":
                    ctrl.BackColor = Color.Red;
                    break;
                case "Green":
                    ctrl.BackColor = Color.Yellow;
                    break;
                case "Yellow":
                    ctrl.BackColor = Color.Black;
                    break;
                default:
                    ctrl.BackColor = Color.Black;
                    break;
            }
        }

        void SaveEventHandler(object sender, EventArgs e)
        {
            SaveToFile();
        }

        private const string filePath = @"d:\test.txt";
        private void LoadFromFile()
        {
            if (!System.IO.File.Exists(filePath))
                return;

            byte[] data = System.IO.File.ReadAllBytes(filePath);
            if (data == null || data.Length != btn.GetLength(0) * btn.GetLength(1) * 2)
                return;

            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);

                    string value = ((char)data[2 * position]).ToString() + ((char)data[2 * position + 1]).ToString();
                    Color color;
                    switch (value)
                    {
                        case "01":
                            color = Color.Red;
                            break;
                        case "00":
                            color = Color.Black;
                            break;
                        case "10":
                            color = Color.Green;
                            break;
                        case "11":
                            color = Color.Yellow;
                            break;
                        default:
                            color = Color.Black;
                            break;
                    }

                    btn[x, y].BackColor = color;
                }
            }
        }

        private void SaveToFile()
        {
            Dictionary<Form1, int> d = new Dictionary<Form1, int>();

            byte[] data = new byte[btn.GetLength(0) * btn.GetLength(1) * 2]; 
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);
                    string value;
                    switch (btn[x, y].BackColor.Name)
                    {
                        case "Red":
                            value = "01";
                            break;
                        case "Black":
                            value = "00";
                            break;
                        case "Green":
                            value = "10";
                            break;
                        case "Yellow":
                            value = "11";
                            break;
                        default:
                            value = "00";
                            break;
                    }
                    data[2 * position] = (byte)value[0];
                    data[2 * position + 1] = (byte)value[1];
                }
            }

            System.IO.File.WriteAllBytes(filePath, data);
        }
    }
}