//获取文件 private void GetFile(WebServiceMessage.Messages msg) { DialogResult dr = MessageBox.Show("好友" + msg.Sender + "给你发来一个文件(" + msg.FileFullName + ",Size:" + msg.Details.Length + "Byte.)。\r\n是否接受该文件?", "接收文件", MessageBoxButtons.YesNo); //如果用户不接受该文件,放弃本次文件传递 if (dr != DialogResult.Yes) { return; } //设置文件名称 string filename = this.GetNewFilename(msg.FileFullName); byte[] fileContent = ConvertStringAndBytes.ConvertStringToBytes(msg.Details); //创建一个文件流对象,并初始化 FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); //向文件流中写入内容 fs.Write(fileContent, 0, fileContent.Length); //关闭流 fs.Close(); }
private void SendFile(string filename, string target) { //创建一个文件流对象 FileStream fs = new FileStream(filename, FileMode.Open); //创建一个二进制组 byte[] bs = new byte[fs.Length]; //从文件流中读出内容 fs.Read(bs, 0, bs.Length); //关闭流 fs.Close(); //初始化Messages对象 WebServiceMessage.Messages msg = new WebServiceMessage.Messages(); msg.Classify = 2; msg.Details = ConvertStringAndBytes.ConvertBytesToString(bs); msg.Receiver = target; msg.FileFullName = filename.Substring(filename.LastIndexOf("\\")); //执行发送操作 WsMessage.SendFile(msg); }