public void Download(Fileinfo file) { FileInfo info = new FileInfo(file.Destination); if (info.Exists) { return; // info.Delete(); } FtpWebRequest request = (FtpWebRequest)WebRequest.Create(file.CompleteFileName); request.Method = WebRequestMethods.Ftp.DownloadFile; request.UseBinary = true; request.Credentials = new NetworkCredential(_username, _password); //FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //Stream responseStream = response.GetResponseStream(); //StreamReader reader = new StreamReader(responseStream); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream)) { using (StreamWriter destination = new StreamWriter(file.Destination)) { destination.Write(reader.ReadToEnd()); destination.Flush(); } } } } //using (Stream rs = response.GetResponseStream()) //{ // using (FileStream fs = info.OpenWrite()) // { // try // { // int bufferSize = 2048; // byte[] bytes = new byte[bufferSize]; // int totBytes = 0; // int read = 0; // do // { // if (rs != null) read = rs.Read(bytes, 0, bufferSize); // fs.Write(bytes, 0, read); // totBytes += read; // //TODO: Raise status event to notify the bytes transferred // OnDownloadProgress(file.CompleteFileName, totBytes); // } while (read > 0 ); // rs?.Flush(); // OnDownloadComplete(file.CompleteFileName); // } // catch (Exception ex) // { // OnStatusChange("Error occured: " + ex.Message, 0, 0); // } // finally // { // fs.Close(); // rs?.Close(); // } // } //} }
public void Upload(Fileinfo file) { FileInfo info = new FileInfo(file.CompleteFileName); string filename = file.CompleteFileName.Substring(file.CompleteFileName.LastIndexOf(@"\") + 1); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(file.Destination + "/" + filename); request.Credentials = new NetworkCredential(_username, _password); request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; request.ContentLength = info.Length; int bufferSize = 2048; byte[] bytes = new byte[bufferSize]; int read = 0; long totBytes = 0; Stream rs = request.GetRequestStream(); using (FileStream fs = info.OpenRead()) { try { do { read = fs.Read(bytes, 0, bufferSize); rs.Write(bytes, 0, read); totBytes += read; //TODO: Raise status event to notify the bytes transferred OnUploadProgress(file.CompleteFileName, totBytes); } while (read == bufferSize); OnUploadComplete(file.CompleteFileName); } catch { } finally { fs.Close(); } } rs.Close(); request = null; return; }
public string CreateRemoteDirectory(Fileinfo file) { //System.IO.FileInfo info = new FileInfo(file.completeFileName); //This is actually a directory: // string filename = file.CompleteFileName;//.Substring(file.completeFileName.LastIndexOf(@"\") + 1); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(file.Destination); request.Credentials = new NetworkCredential(_username, _password); request.Method = WebRequestMethods.Ftp.MakeDirectory; request.UseBinary = true; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); return response.StatusDescription; //request.UseBinary = true; //request.ContentLength = info.Length; }