public void GetFileList() { if (_tcp == null) return; if (_listBox == null) return; NetworkStream stream = _tcp.GetStream(); try { TCommand command = new TCommand(CommandStyleEnum.cList); byte[] data = command.ToBytes(); stream.Write(data, 0, data.Length); byte[] recData = new byte[9999]; int recLen = stream.Read(recData, 0, recData.Length); if (recLen == 0) return; command = new TCommand(recData, recLen); if (command.commandStyle != CommandStyleEnum.cListReturn) return; dd a = delegate() { _listBox.Items.Clear(); for (int i = 0; i < command.argList.Count; i++) { _listBox.Items.Add((string)command.argList[i]); } }; _listBox.Invoke(a); } finally { stream.Close(); _tcp.Close(); } }
public void GetFile() { #region 定义变量 FileStream s; long currentSize; int p = 0; #endregion #region 获取当前下载进度 try { s = new FileStream(_path, FileMode.Open, FileAccess.ReadWrite, FileShare.None); currentSize = s.Length; s.Position = currentSize; p = Convert.ToInt32(((double)currentSize / (double)_fileLength) * 100); dd a = delegate() { _p.Value = p; }; _p.Invoke(a); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } #endregion #region 判断文件是否已经下载完毕 if (currentSize == _fileLength) { MessageBox.Show("文件已经下载完毕!"); s.Close(); return; } #endregion #region 开始下载 NetworkStream stream = _tcp.GetStream(); try { TCommand command = new TCommand(CommandStyleEnum.cGetFile); command.AppendArg(_fileName); command.AppendArg(currentSize.ToString()); byte[] data = command.ToBytes(); stream.Write(data, 0, data.Length); while (true) { byte[] recData = new byte[1024]; int recLen = stream.Read(recData, 0, 1024); if (recLen == 0)//断开 return; s.Write(recData, 0, recLen); currentSize += recLen; p = Convert.ToInt32(((double)currentSize / (double)_fileLength) * 100); if (p != _p.Value) { dd a = delegate() { _p.Value = p; Application.DoEvents(); }; _p.Invoke(a); } if (currentSize == _fileLength) { MessageBox.Show("下载完毕!"); return; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { #region 释放 stream.Close(); _tcp.Close(); s.Close(); #endregion } #endregion }
private long GetFileLength(string fileName) { TcpClient tcp = null; NetworkStream stream = null; try { #region 建立连接,与发送请求文件长度的命令 tcp = new TcpClient(); tcp.Connect(_server, _port); stream = tcp.GetStream(); TCommand command = new TCommand(CommandStyleEnum.cGetFileLength); command.AppendArg(fileName); byte[] data = command.ToBytes(); stream.Write(data, 0, data.Length); #endregion #region 接收数据 byte[] recData = new byte[1024]; int recLen = stream.Read(recData, 0, recData.Length); command = new TCommand(recData, recLen); #endregion #region 转换文件长度 if (command.commandStyle != CommandStyleEnum.cGetFileLengthReturn) return 0; if (command.argList.Count == 0) return 0; long fileLength = 0; try { fileLength = Convert.ToInt64((string)command.argList[0]); } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; } #endregion return fileLength; } catch (Exception ex) { MessageBox.Show(ex.Message); return 0; } finally { #region 释放 if (stream != null) stream.Close(); if (tcp != null) tcp.Close(); #endregion } }