C# 學習記錄(一)

1.UDP

引用空間

using System.Net.Sockets;

using System.Net;

Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.Bind(new IPEndPoint(IPAddress.Parse(“192.168.0.1”,8080);
IPEndPoint point = new IPEndPoint(IPAddress.Parse(“192.168.0.1”,8081);
client.SendTo(data, point);    //發送到目的地址

client.Close();

2.使用線程

                        ThreadStart t1 = new ThreadStart(UDP_Send);
                        Thread thread = new Thread(t1);

                        thread.Start();

在線程中使用定時器要用invoke

                        this.Invoke(new MethodInvoker(delegate
                        {
                            timer1.Start();

                        }));

3.打開文件FileStream

 OpenFileDialog dialog = new OpenFileDialog();
 dialog.Multiselect = true;//該值確定是否可以選擇多個文件
 dialog.Title = "請選擇文件夾";
 dialog.Filter = "所有文件(*.*)|*.*|bin(*.bin)|*.bin";
 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 {
                    string[] file = dialog.FileNames;
                    for (int i = 0; i < file.Length; i++)
                    {
                        listBox1.Items.Add(file[i].Substring(file[i].LastIndexOf("\\") + 1)); //取地址,可選多個
                        FileStream fs = new FileStream(file[i], FileMode.OpenOrCreate, FileAccess.Read);
                        BinaryReader bw = new BinaryReader(fs);
                        byte[] data = bw.ReadBytes((int)bw.BaseStream.Length);
                        fs.Close();
                        bw.Close();//打開了要關閉

                    }

}

4.寫入文件FileStream

                string path = Application.StartupPath.ToString();//選擇軟件目錄,不帶\\
                FileStream fs = new FileStream(path + "\\UDP_config.bin", FileMode.Create);
                //獲得字節數組
                byte[] data = new byte[16];
                data[0]=0;//...
                fs.Write(data, 0, data.Length);
                //清空緩衝區、關閉流
                fs.Flush();

                fs.Close();

5.防止跨線程操作

  Control.CheckForIllegalCrossThreadCalls = false;//防止跨線程操作報錯,不提倡這樣做 

6.快捷鍵操作

menuStrip上纔有快捷鍵,將上面的text設置快捷鍵,並隱藏,用button1_Click(null,null)調用其他事件。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章