예제 #1
0
        public static TCommand GetFileListCommand()
        {
            TSysConfig sysConfig = new TSysConfig();
            string path = sysConfig.GetIniString("path", "-1");
            if (path == "-1")
            {
                MessageBox.Show("没有从系统配置文件中找到目录");
                return null;
            }

            string[] files = GetFileList(path);
            TCommand command = new TCommand(CommandStyleEnum.cListReturn);

            foreach (string s in files)
                command.AppendArg(s);

            return command;
        }
        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
        }
예제 #3
0
        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
            }
        }