bool DirectoryExists(Dictionary <string, List <string> > directoryCache, string dirString)
        {
            if (dirString == null || dirString.Length == 0)
            {
                return(true);
            }
            string parentDir = ParentPath(dirString);

            if (!directoryCache.ContainsKey(parentDir))
            {
                IFtpWebRequest request  = CreateRequest(FtpPath(parentDir), WebRequestMethods.Ftp.ListDirectory);
                StreamReader   response = new StreamReader(request.GetResponseStream());
                directoryCache.Add(parentDir, new List <string>());
                while (!response.EndOfStream)
                {
                    string line = response.ReadLine();
                    directoryCache[parentDir].Add(line);
                }
                response.Close();
            }
            string dirName = NamePath(dirString);

            return(directoryCache[parentDir].Exists(delegate(string listLine) {
                return listLine == dirName ||
                listLine.EndsWith("/" + dirName) ||
                listLine.EndsWith("\\" + dirName);
            }));
        }
Exemple #2
0
        public static void SetFtpMode(this IFtpWebRequest request, FtpMode ftpMode)
        {
            Debug.Assert(request != null);

            switch (ftpMode)
            {
            case FtpMode.Passive:
                request.UsePassive = true;
                break;

            case FtpMode.Active:
                request.UsePassive = false;
                break;

            default:
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                  "FTP Type '{0}' is not valid.", ftpMode));
            }
        }
 public FtpWebOperation(IFtpWebRequest webRequest)
 {
     WebRequest = webRequest;
 }
Exemple #4
0
        private bool UploadFile(string localFilePath, string remoteFilePath, bool errorOnFailure)
        {
            Uri      uri       = FtpPath(remoteFilePath);
            FileInfo localFile = new FileInfo(localFilePath);

            if (!localFile.Exists)
            {
                Log.LogError(Properties.Resources.FtpLocalNotFound, localFile);
                return(false);
            }

            Log.LogMessage(Properties.Resources.FtpUploading, localFilePath, uri);

            IFtpWebRequest request = CreateRequest(uri, WebRequestMethods.Ftp.UploadFile);

            request.SetContentLength(localFile.Length);

            const int bufferLength = 2048;

            byte[] buffer          = new byte[bufferLength];
            int    readBytes       = 0;
            long   totalBytes      = localFile.Length;
            long   progressUpdated = 0;
            long   wroteBytes      = 0;

            try {
                Stopwatch watch = Stopwatch.StartNew();
                using (Stream fileStream = localFile.OpenRead(),
                       requestStream = request.GetRequestStream()) {
                    do
                    {
                        readBytes = fileStream.Read(buffer, 0, bufferLength);
                        requestStream.Write(buffer, 0, readBytes);
                        wroteBytes += readBytes;

                        // log progress every 5 seconds
                        if (watch.ElapsedMilliseconds - progressUpdated > 5000)
                        {
                            progressUpdated = watch.ElapsedMilliseconds;
                            Log.LogMessage(MessageImportance.Normal,
                                           Properties.Resources.FtpPercentComplete,
                                           wroteBytes * 100 / totalBytes,
                                           ByteDescriptions.FormatBytesPerSecond(wroteBytes, watch.Elapsed.TotalSeconds, 1));
                        }
                    }while (readBytes != 0);
                }
                watch.Stop();

                string statusDescription = request.GetStatusDescriptionAndCloseResponse();
                Log.LogMessage(MessageImportance.Low, Properties.Resources.FtpUploadComplete, statusDescription);
                Log.LogMessage(Properties.Resources.FtpTransfered,
                               ByteDescriptions.FormatByte(totalBytes, 1),
                               ByteDescriptions.FormatBytesPerSecond(totalBytes, watch.Elapsed.TotalSeconds, 1),
                               watch.Elapsed.ToString());
            } catch (Exception ex) {
                if (errorOnFailure)
                {
                    Log.LogErrorFromException(ex);
                }
                else
                {
                    Log.LogMessage("Exception occurred trying to upload: " + ex.Message);
                }
                return(false);
            }

            return(true);
        }
Exemple #5
0
        private void CreateDirectory(string dir)
        {
            IFtpWebRequest rq = CreateRequest(FtpPath(dir), WebRequestMethods.Ftp.MakeDirectory);

            rq.GetAndCloseResponse();
        }
 public FtpWebOperation(IFtpWebRequest webRequest)
 {
     WebRequest = webRequest;
 }