public bool Download(FileState fileState) { tot = 0; float all = fileState.getTot(); string DestnationPath = GetSaveFilePath(fileState.Filename); try { string uri = string.Empty; if (currentDir == "/") { uri = ftpUristring; } else { uri = ftpUristring + currentDir; } uri += "/" + projectid + "/" + fileState.Filename; FtpWebRequest request = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.DownloadFile); FtpWebResponse response = GetFtpResponse(request); if (response == null) { //AddInfo("服务器未响应..."); return(false); } Stream responseStream = response.GetResponseStream(); FileStream filestream = File.Create(DestnationPath); int buflength = 8196; byte[] buffer = new byte[buflength]; int bytesRead = 1; while (bytesRead != 0) { bytesRead = responseStream.Read(buffer, 0, buflength); filestream.Write(buffer, 0, bytesRead); tot += bytesRead; fileState.DownloadState = String.Format("{0:F1}", tot * 100.0f / all) + "%"; } responseStream.Close(); filestream.Close(); return(true); } catch (WebException ex) { return(true); } }
//如果文件不存在,则从服务器下载相应的文件 public bool CheckFileExistAsync(string projectID) { ftp = new FTP(projectID); Dictionary <string, int> fileDict = ftp.GetFileList(); existFile = new Dictionary <string, int>(); NoFile = new Dictionary <string, int>(); foreach (string filename in fileDict.Keys) { if (File.Exists(GetFilePath(filename, projectID))) { existFile[filename] = fileDict[filename]; } else { NoFile[filename] = fileDict[filename]; } } int index = 1; foreach (string key in NoFile.Keys) { FileState file = new FileState() { ID = index, Filename = key, FileLength = String.Format("{0:F1}", fileDict[key] * 1.0f / 1024 / 1024) + "M", DownloadState = "0.00%" }; file.setTot(fileDict[key]); _fileList.Add(file); index++; } DownloadDG.ItemsSource = _fileList; if (NoFile.Count == 0) { return(true); } else { return(false); } }