Exemplo n.º 1
0
 /// <summary>
 /// 得到文件列表
 /// </summary>
 /// <returns></returns>
 public string[] GetList(string strPath)
 {
     if (ftp == null)
     {
         ftp = this.getFtpClient();
     }
     ftp.Connect();
     ftp.ChDir(strPath);
     return(ftp.Dir("*"));
 }
Exemplo n.º 2
0
            /// <summary>
            /// Function to start a download
            /// </summary>
            /// <param name="orgremoteFile"></param>
            /// <param name="remotefile"></param>
            /// <param name="localfile"></param>
            public void Download(string orgremoteFile, string remotefile, string localfile)
            {
                string name   = System.IO.Path.GetFileName(remotefile);
                string folder = orgremoteFile.Substring("remote:".Length);

                folder = folder.Substring(0, folder.Length - (name.Length + 1));
                string[] subitems = folder.Split(new char[] { '?' });
                if (subitems[4] == string.Empty)
                {
                    subitems[4] = "/";
                }
                bool   fileExists = false;
                string tmpDir     = string.Empty;

                FTPFile[] files;
                try
                {
                    tmpDir = subitems[4];
                    Connection.ChDir(tmpDir);
                    files = Connection.DirDetails(); //(tmpDir);
                    for (int i = 0; i < files.Length; ++i)
                    {
                        FTPFile file = files[i];
                        if (file.Dir)
                        {
                            continue;
                        }
                        if (String.Compare(file.Name, name, true) == 0)
                        {
                            fileExists = true;
                            break;
                        }
                    }
                    if (!fileExists)
                    {
                        if (Connection.Connected)
                        {
                            Connection.QuitImmediately();
                        }
                        Busy = false;
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("FtpConnectionCache:Download {0}", ex.Message);
                    return;
                }

                Log.Debug("FTPConnection: Download {0} from {1} to {2}", remotefile, tmpDir, localfile);
                LocalFileName          = localfile;
                RemoteFileName         = remotefile;
                OriginalRemoteFileName = orgremoteFile;
                BackgroundWorker worker = new BackgroundWorker();

                worker.DoWork += new DoWorkEventHandler(StartDownLoad);
                worker.RunWorkerAsync();
            }
        private void ImageUploadForm_Load(object sender, EventArgs e)
        {
            //每天在服务器创建一个文件夹
            string DirectoryName = PaCSGlobal.GetServerDateTime(2);

            string[] str    = ftpClient.Dir("");
            bool     exists = ((IList)str).Contains(DirectoryName + "\r");

            if (!exists)//如果ftp服务器上没有创建,则创建新文件夹
            {
                ftpClient.MkDir(DirectoryName);
            }
            ftpClient.ChDir(DirectoryName);
        }
Exemplo n.º 4
0
        [TestMethod]//功能弱
        public void MyFtpTest()
        {
            //string host = "121.15.166.119";
            //string userName = "******";
            //string userPwd = "0_1@32&*s+udd~QhfsVfEW4*5<)";
            //var ftp = new FTPClient(host, 22);

            string host     = "115.159.186.113";
            string userName = "******";
            string userPwd  = "Djy123456Djy";
            var    ftp      = new FTPClient(host, 21);


            ftp.ControlEncoding = Encoding.GetEncoding("GBK");//设置编码

            ftp.Login(userName, userPwd);
            ftp.ConnectMode  = FTPConnectMode.PASV;
            ftp.TransferType = FTPTransferType.BINARY;

            string[] mm = ftp.Features();

            //获取文件列表
            string[] files = ftp.Dir();
            //上传文件
            //ftp.Put("error.txt", "error.txt");
            ftp.ChDir(files[1]);// ("61_平安银行银企直连接入规范文档(对客户)");
            files = ftp.Dir();
            //下载文件
            // ftp.Get(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "端口转发.exe");
            string strFile = files[12];

            //var bb = ftp.Get(strFile);
            MemoryStream ms = new MemoryStream();

            ftp.Get(ms, strFile);
            byte[] bb = ms.ToArray();

            //删除文件
            //ftp.Delete("Demo.cs");

            ftp.Quit();
        }
Exemplo n.º 5
0
        /**/
        /// <summary>
        /// 在下载结束后清空程序目录的多余文件
        /// </summary>
        private static void ClearFolder()
        {
            string folder = Environment.CurrentDirectory;
            string[] dictorys = Directory.GetFiles(folder);
            foreach (string dictory in dictorys)
            {
                FileInfo info = new FileInfo(dictory);
                if (info.Length == 0)
                    File.Delete(dictory);
            }
        }


        /**/
        /// <summary>
        /// 递归获取ftp文件夹的内容
        /// </summary>
        /// <param name="fileMark">文件标记</param>
        /// <param name="path">远程路径</param>
        /// <param name="client"></param>
        /// <param name="folder"></param>
        private static void GetFolder(string fileMark, string path, FTPClient client, string folder, DateTime currentDate)
        {
            string[] dirs = client.Dir(path);  //获取目录下的内容
            client.ChDir(path);  //改变目录
            foreach (string dir in dirs)
            {
                string[] infos = dir.Split(' ');
                //string info = infos[infos.Length - 1].Replace("\r", "");
                string info = getFileName(dir.Replace("\r", ""));

                if (dir.StartsWith("d") && !string.IsNullOrEmpty(dir))  //为目录
                {

                    if (!info.EndsWith(".") && !info.EndsWith(".."))  //筛选出真实的目录
                    {
                        Directory.CreateDirectory(folder + "\\" + info);
                        GetFolder(fileMark, path + "/" + info, client, folder + "\\" + info, currentDate);
                        client.ChDir(path);
                    }
                }
                else if (dir.StartsWith("-r"))  //为文件
                {
                    string file = folder + "\\" + info;
                    if (File.Exists(file))
                    {
                        long remotingSize = client.GetFileSize(info);
                        FileInfo fileInfo = new FileInfo(file);
                        long localSize = fileInfo.Length;

                        if (remotingSize != localSize)  //短点续传
                        {
                            client.GetBrokenFile(info, folder, info, localSize);
                        }
                    }
                    else
                    {
                        //由于FTP有滞后,所以需要多取一天的数据
                        if (info.StartsWith("BRK.PCNT.Site Name." + currentDate.ToString("yyMMdd"))
                            || info.StartsWith("BRK.PCNT.Site Name." + currentDate.AddDays(1).ToString("yyMMdd")))
                        {
                            client.GetFile(info, folder, info);  //下载文件
                            Console.WriteLine("文件" + folder + info + "已经下载");
                        }
                    }
                }
            }

        }
Exemplo n.º 6
0
        public void Ftp(List <string> zipFiles)
        {
            IEnumerable <XElement> ftps = gConfig.GetElements("Backup/Ftp");

            foreach (XElement xeFtp in ftps)
            {
                if (gAbortTask)
                {
                    break;
                }
                string ftpServer = xeFtp.zAttribValue("server");
                if (ftpServer == null)
                {
                    //cTrace.Trace("Ftp          : server is'nt defined");
                    gTaskTrace.WriteLine("Ftp          : server is'nt defined");
                    continue;
                }
                string ftpUser = xeFtp.zAttribValue("user");
                if (ftpUser == null)
                {
                    //cTrace.Trace("Ftp          : user is'nt defined");
                    gTaskTrace.WriteLine("Ftp          : user is'nt defined");
                    continue;
                }
                string ftpPassword  = xeFtp.zAttribValue("password");
                string ftpDirectory = xeFtp.zAttribValue("directory");

                string dir = "";
                if (ftpDirectory != null)
                {
                    dir = " directory " + ftpDirectory;
                }
                gTaskTrace.WriteLine("Ftp          : connect to server {0}{1}", ftpServer, dir);

                FTPClient ftp = new FTPClient();
                try
                {
                    ftp.RemoteHost = ftpServer;
                    ftp.Connect();
                    ftp.Login(ftpUser, ftpPassword);
                    if (ftpDirectory != null)
                    {
                        ftp.ChDir(ftpDirectory);
                    }
                    ftp.TransferType      = FTPTransferType.BINARY;
                    ftp.BytesTransferred += new BytesTransferredHandler(FtpBytesTransferred);


                    int iFile = 0;
                    foreach (string zipFile in zipFiles)
                    {
                        if (gAbortTask)
                        {
                            break;
                        }
                        gTaskProgress.SetProgressText("Ftp copy file " + zipFile);
                        gTaskProgress.SetProgress(++iFile, zipFiles.Count);
                        gTaskTrace.WriteLine("Ftp          : copy file {0}", zipFile);

                        if (!zFile.Exists(zipFile))
                        {
                            //cTrace.Trace("Ftp          : file does'nt exist {0}", zipFile);
                            gTaskTrace.WriteLine("Ftp          : file does'nt exist {0}", zipFile);
                            continue;
                        }
                        string remoteFile = zPath.GetFileName(zipFile);
                        //glFtpByteToTransfer = new FileInfo(zipFile).Length;
                        glFtpByteToTransfer = zFile.CreateFileInfo(zipFile).Length;
                        gTaskProgressDetail.SetProgressText("FTP transfer file {0}", remoteFile);

                        try
                        {
                            ftp.Put(zipFile, remoteFile);
                        }
                        catch
                        {
                            if (!gbErrorGeneratedByFtpStop)
                            {
                                throw;
                            }
                            //string sError = cError.GetErrorMessage(ex, false, true);
                            //gTrace.Trace(sError);
                            break;
                        }

                        gTaskProgressDetail.SetProgress(glFtpByteToTransfer, glFtpByteToTransfer);
                    }

                    ftp.BytesTransferred -= new BytesTransferredHandler(FtpBytesTransferred);
                }
                finally
                {
                    if (!gbErrorGeneratedByFtpStop)
                    {
                        ftp.Quit();
                    }
                    else
                    {
                        gbErrorGeneratedByFtpStop = false;
                    }
                }
            }
        }