Exemple #1
16
        private static void backup(FTPClient ftp, DirectoryInfo dir, Regex regex, RegistryKey key)
        {
            string[] files = ftp.Dir(null, true);
                string[] names = new string[files.Length];
                int i = 0;
                foreach (string file in files)
                {
                    names[i] = file.Substring(56);
                    if (file.Substring(0,1) == "d")
                    {
                        // A directory!
                        if (dir.GetDirectories(names[i]).Length != 1 && !names[i].Substring(0,1).Equals("."))
                            RecRm(ftp, names[i]);
                    }
                    else
                        // A file!
                        if (regex.IsMatch(names[i]) && dir.GetFiles(names[i]).Length != 1)
                            ftp.Delete(names[i]);
                    i++;
                }

                foreach (DirectoryInfo subdir in dir.GetDirectories())
                {
                    if (!stringSearch(names, subdir.Name))
                        ftp.MkDir(subdir.Name);

                    ftp.ChDir(subdir.Name);
                    backup(ftp, subdir, regex, getDataKey(ref key, subdir.Name));
                    ftp.ChDir("..");
                }

                foreach (FileInfo file in dir.GetFiles())
                {
                    if (!regex.IsMatch(file.Name))
                        continue;

                    if (file.LastWriteTime > getDateTime(key.GetValue(file.Name, "0001-01-01 00:00:00").ToString()))
                    {
                        ftp.Put(file.FullName, file.Name, false);
                        key.SetValue(file.Name, DateTime.Now.ToString());
                    }
                }
        }
Exemple #2
0
 private static void RecRm(FTPClient ftp, string name)
 {
     ftp.ChDir(name);
     string[] list;
     if ((list = ftp.Dir(null, true)).Length > 0)
     {
         foreach (string thing in list)
         {
             string thingname = thing.Substring(56);
             if (thing.Substring(0,1) == "d")
                 RecRm(ftp, thingname);
             else
                 ftp.Delete(thingname);
         }
     }
     ftp.ChDir("..");
     ftp.RmDir(name);
 }
        /// <summary>   
        /// Test harness
        /// </summary>
        public static void Main(string[] args)
        {
            // we want remote host, user name and password
            if (args.Length < 7)
            {
                log.Debug(Convert.ToString(args.Length));
                Usage();
                System.Environment.Exit(1);
            }
            try
            {
                // assign args to make it clear
                string host = args[0];
                string user = args[1];
                string password = args[2];
                string filename = args[3];
                string directory = args[4];
                string mode = args[5];
                string connMode = args[6];

                FTPClient ftp = new FTPClient();
                ftp.RemoteHost = host;
                ftp.ControlPort = 21;

                // set up message collector
                ftp.CommandSent += new FTPMessageHandler(FTPClientTest.LogCommand);
                ftp.ReplyReceived += new FTPMessageHandler(FTPClientTest.LogReply);
                ftp.TransferStarted += new EventHandler(FTPClientTest.LogTransferStarted);
                ftp.TransferComplete += new EventHandler(FTPClientTest.LogTransferComplete);

                // connect
                ftp.Connect();
                ftp.Login(user, password);
                ftp.Quit();

                // connect again
                ftp = new FTPClient(host);
                ftp.CommandSent += new FTPMessageHandler(FTPClientTest.LogCommand);
                ftp.ReplyReceived += new FTPMessageHandler(FTPClientTest.LogReply);
                ftp.TransferStarted += new EventHandler(FTPClientTest.LogTransferStarted);
                ftp.TransferComplete += new EventHandler(FTPClientTest.LogTransferComplete);
                ftp.BytesTransferred += new BytesTransferredHandler(FTPClientTest.BytesTransferred);

                ftp.Login(user, password);

                // binary transfer
                if (mode.ToUpper().Equals("BINARY".ToUpper()))
                {
                    ftp.TransferType = FTPTransferType.BINARY;
                }
                else if (mode.ToUpper().Equals("ASCII".ToUpper()))
                {
                    ftp.TransferType = FTPTransferType.ASCII;
                }
                else
                {
                    log.Debug("Unknown transfer type: " + args[5]);
                    System.Environment.Exit(- 1);
                }

                // PASV or active?
                if (connMode.ToUpper().Equals("PASV".ToUpper()))
                {
                    ftp.ConnectMode = FTPConnectMode.PASV;
                }
                else if (connMode.ToUpper().Equals("ACTIVE".ToUpper()))
                {
                    ftp.ConnectMode = FTPConnectMode.ACTIVE;
                }
                else
                {
                    log.Debug("Unknown connect mode: " + args[6]);
                    System.Environment.Exit(- 1);
                }

                // change dir
                ftp.ChDir(directory);

                // Put a local file to remote host
                ftp.Put(filename, filename);

                // get bytes
                byte[] buf1 = ftp.Get(filename);
                log.Debug("Got " + buf1.Length + " bytes");

                // append local file
                try
                {
                    ftp.Put(filename, filename, true);
                }
                catch (FTPException ex)
                {
                    log.Debug("Append failed: " + ex.Message);
                }

                // get bytes again - should be 2 x
                //byte[] buf2 = ftp.Get(filename);
                //log.Debug("Got " + buf2.Length + " bytes");

                // rename
                ftp.Rename(filename, filename + ".new");

                // get a remote file - the renamed one
                ftp.Get(filename + ".tst", filename + ".new");

                // delete the remote file
                ftp.Delete(filename + ".new");

                // ASCII transfer
                ftp.TransferType = FTPTransferType.ASCII;

                // test that dir() works in full mode
                string[] listings = ftp.Dir(".", true);
                for (int i = 0; i < listings.Length; i++)
                    log.Debug(listings[i]);

                // and now DirDetails test
                FTPFile[] files = ftp.DirDetails(".");
                log.Debug(files.Length + " files");
                for (int i = 0; i < files.Length; i++) {
                    log.Debug(files[i].ToString());
                }

                // try system()
                log.Debug(ftp.GetSystem());

                // try pwd()
                log.Debug(ftp.Pwd());

                ftp.Quit();

                log.Debug("******** message log ********");
                log.Debug(FTPClientTest.messages.ToString());
            }
            catch (SystemException ex)
            {
                log.Debug("Caught exception: " + ex.Message);
            }
            catch (FTPException ex)
            {
                log.Debug("Caught exception: " + ex.Message);
            }
        }