Exemplo n.º 1
0
        private void pictureBox6_Click(object sender, EventArgs e)//文件传输
        {
            try
            {
                string wenjianluj = Wenjiancaozuo.lujin();
                //原始的字符串转换成的字节数组
                string fileName      = System.IO.Path.GetFileName(wenjianluj);
                string fileExtension = System.IO.Path.GetExtension(wenjianluj);

                byte[] buffe = new byte[2048];
                buffe = Encoding.Default.GetBytes("向你发送了一个文件:" + fileName);
                byte[] result = new byte[buffe.Length + 1];
                //头部协议字节 1:代表字符串
                result[0] = 1;
                Buffer.BlockCopy(buffe, 0, result, 1, buffe.Length);
                socketSend.Send(result);
                Thread.Sleep(1000);

                List <byte> list = new List <byte>();
                //获取要发送的文件的路径
                string strPath = wenjianluj.Trim();
                long   lSize   = new FileInfo(strPath).Length;
                MessageBox.Show(lSize.ToString());
                using (FileStream sw = new FileStream(strPath, FileMode.Open, FileAccess.Read))
                {
                    byte[] buffer = new byte[2048 * 10000];
                    int    r      = sw.Read(buffer, 0, buffer.Length);
                    list.Add(2);
                    list.AddRange(buffer);
                }
                byte[] newBuffer = list.ToArray();
                socketSend.Send(newBuffer);
                if (richTextBox1.Text == "")
                {
                    richTextBox1.Text = WName + DateTime.Now + "\n文件:" + fileName;
                }
                else
                {
                    richTextBox1.Text = richTextBox1.Text + "\n" + WName + DateTime.Now + "\n文件:" + fileName;
                }
                tianjiajilu("文件:" + fileName);
                MessageBox.Show("文件发送成功");
            }
            catch (Exception)
            {
            }
        }
 private void button5_Click(object sender, EventArgs e)
 {
     Slave2MainDele.Invoke(Wenjiancaozuo.lujin());
 }