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
using System.IO;
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)调用其他事件。