Exemplo n.º 1
0
        public static List <string> FtpList(string ftpUrl, string ftpPath, string ftpUsername, string ftpPassword, bool usePassive = false, string searchRegEx = ".*")
        {
            System.Net.FtpWebRequest request = FtpRequest(
                ftpUrl: ftpUrl,
                ftpPath: ftpPath,
                ftpUsername: ftpUsername,
                ftpPassword: ftpPassword,
                ftpMethod: System.Net.WebRequestMethods.Ftp.ListDirectory,
                usePassive: usePassive
                );

            using (System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        List <string> lst_strFiles = new List <string>();
                        while (!reader.EndOfStream)
                        {
                            var line = reader.ReadLine();
                            if (Regex.Match(line, searchRegEx).Success)
                            {
                                lst_strFiles.Add(line);
                            }
                        }
                        return(lst_strFiles);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static System.Net.FtpWebResponse FtpDownload(string localFilePath, string ftpUrl, string ftpPath, string ftpUsername, string ftpPassword, bool usePassive = false)
        {
            System.Net.FtpWebRequest request = FtpRequest(
                ftpUrl: ftpUrl,
                ftpPath: ftpPath,
                ftpUsername: ftpUsername,
                ftpPassword: ftpPassword,
                ftpMethod: System.Net.WebRequestMethods.Ftp.DownloadFile,
                usePassive: usePassive
                );

            using (System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        using (StreamWriter destination = new StreamWriter(localFilePath))
                        {
                            destination.Write(reader.ReadToEnd());
                            destination.Flush();
                        }
                    }
                }
                return(response);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// FTP下载
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="remotepath">远程路径</param>
        /// <param name="localpath">本地路径</param>
        public bool FtpDownload(string userName, string password, string remotepath, string localpath)
        {
            bool bRet    = true;
            var  request = System.Net.WebRequest.Create(remotepath) as System.Net.FtpWebRequest;

            request.Method      = System.Net.WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new System.Net.NetworkCredential(userName, password);
            request.KeepAlive   = false;
            request.UseBinary   = true;

            System.Net.FtpWebResponse response  = null;
            System.IO.Stream          resStream = null;
            System.IO.FileStream      outStream = null;
            try
            {
                response  = request.GetResponse() as System.Net.FtpWebResponse;
                resStream = response.GetResponseStream();

                outStream = new System.IO.FileStream(localpath, System.IO.FileMode.Create);

                byte[] tmpBuf     = new byte[2048];
                int    nReadCount = resStream.Read(tmpBuf, 0, tmpBuf.Length);
                while (nReadCount > 0)
                {
                    outStream.Write(tmpBuf, 0, nReadCount);
                    nReadCount = resStream.Read(tmpBuf, 0, tmpBuf.Length);
                }
                tmpBuf = null;
            }
            catch (Exception e)
            {
                bRet = false;
                System.Console.WriteLine("FtpDownload Exception: " + e.Message);
                Settings.RuntimeLog.Severe("FtpDownload Exception: " + e.ToString());
            }
            finally
            {
                if (outStream != null)
                {
                    outStream.Flush();
                    outStream.Close();
                    outStream.Dispose();
                    outStream = null;
                }
                if (resStream != null)
                {
                    resStream.Close();
                    resStream.Dispose();
                    resStream = null;
                }
                if (response != null)
                {
                    response.Close();
                    response.Dispose();
                    response = null;
                }
            }
            return(bRet);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="downFile"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public Boolean download(string url, string downFile, string username, string password)
        {
            Boolean ret = true;

            try
            {
                //ダウンロードするファイルのURI
                Uri u = new Uri(url);

                //FtpWebRequestの作成
                System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)
                                                  System.Net.WebRequest.Create(u);
                //ログインユーザー名とパスワードを設定
                ftpReq.Credentials = new System.Net.NetworkCredential(username, password);
                //MethodにWebRequestMethods.Ftp.DownloadFile("RETR")を設定
                ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
                //要求の完了後に接続を閉じる
                ftpReq.KeepAlive = false;
                //ASCIIモードで転送する
                ftpReq.UseBinary = true;
                //PASSIVEモードを無効にする
                ftpReq.UsePassive = false;

                //FtpWebResponseを取得
                System.Net.FtpWebResponse ftpRes =
                    (System.Net.FtpWebResponse)ftpReq.GetResponse();
                //ファイルをダウンロードするためのStreamを取得
                System.IO.Stream resStrm = ftpRes.GetResponseStream();
                //ダウンロードしたファイルを書き込むためのFileStreamを作成
                System.IO.FileStream fs = new System.IO.FileStream(
                    downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                //ダウンロードしたデータを書き込む
                byte[] buffer = new byte[1024];
                while (true)
                {
                    int readSize = resStrm.Read(buffer, 0, buffer.Length);
                    if (readSize == 0)
                    {
                        break;
                    }
                    fs.Write(buffer, 0, readSize);
                }
                fs.Close();
                resStrm.Close();

                //閉じる
                ftpRes.Close();
            }
            catch (Exception ex)
            {
                NCLogger.GetInstance().WriteExceptionLog(ex);
                ret = false;
            }
            return(ret);
        }
Exemplo n.º 5
0
Arquivo: Lib.cs Projeto: radtek/MMRDS
        public List <string> GetManifestFileList()
        {
            List <string> result = new List <string>();

            // Get the object used to communicate with the server.
            System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(System.Configuration.ConfigurationManager.AppSettings["ftp_site"] + "/m");
            request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["ftp_user_id"], System.Configuration.ConfigurationManager.AppSettings["ftp_password"]);

            System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();

            System.IO.Stream       responseStream = response.GetResponseStream();
            System.IO.StreamReader reader         = new System.IO.StreamReader(responseStream);


            string line = reader.ReadLine();

            while (!string.IsNullOrEmpty(line))
            {
                if (line.StartsWith("-"))
                {
                    int start_index = line.LastIndexOf(' ');
                    result.Add(line.Substring(start_index).Trim());
                }
                line = reader.ReadLine();
            }

            /*
             * drwxrwxrwx   1 user     group           0 Apr 12 11:10 .
             * drwxrwxrwx   1 user     group           0 Apr 12 11:10 ..
             * drwxrwxrwx   1 user     group           0 Apr 12 10:42 Epi_Info_7-1-5
             * -rw-rw-rw-   1 user     group       10193 Apr 12 11:10 release-7.1.5.txt
             *
             */


            // Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);



            reader.Close();
            response.Close();

            return(result);
        }
Exemplo n.º 6
0
Arquivo: Lib.cs Projeto: radtek/MMRDS
        public string GetTextFileContent(string file_name)
        {
            string result = null;

            // Get the object used to communicate with the server.
            System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(System.Configuration.ConfigurationManager.AppSettings["ftp_site"] + "/m/" + file_name);
            request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["ftp_user_id"], System.Configuration.ConfigurationManager.AppSettings["ftp_password"]);

            System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();

            System.IO.Stream       responseStream = response.GetResponseStream();
            System.IO.StreamReader reader         = new System.IO.StreamReader(responseStream);


            result = reader.ReadToEnd();

            reader.Close();
            response.Close();

            return(result);
        }
        public void Exec(Model.Prestashop.PsProductAttachment PsProductAttachment)
        {
            try
            {
                //<YH> 21/08/2012
                string DirAttachment = Global.GetConfig().Folders.RootAttachment;

                Model.Local.ArticleRepository ArticleRepository = new Model.Local.ArticleRepository();
                if (ArticleRepository.ExistPre_Id(Convert.ToInt32(PsProductAttachment.IDProduct)))
                {
                    Model.Local.Article Article = ArticleRepository.ReadPre_Id(Convert.ToInt32(PsProductAttachment.IDProduct));
                    Model.Local.AttachmentRepository AttachmentRepository = new Model.Local.AttachmentRepository();
                    if (AttachmentRepository.ExistPre_IdArt_Id(Convert.ToInt32(PsProductAttachment.IDAttachment), Article.Art_Id) == false)
                    {
                        Model.Prestashop.PsAttachmentRepository     PsAttachmentRepository     = new Model.Prestashop.PsAttachmentRepository();
                        Model.Prestashop.PsAttachment               PsAttachment               = PsAttachmentRepository.ReadAttachment(PsProductAttachment.IDAttachment);
                        Model.Prestashop.PsAttachmentLangRepository PsAttachmentLangRepository = new Model.Prestashop.PsAttachmentLangRepository();
                        if (PsAttachmentLangRepository.ExistAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang))
                        {
                            Model.Prestashop.PsAttachmentLang PsAttachmentLang = PsAttachmentLangRepository.ReadAttachmentLang(PsAttachment.IDAttachment, Core.Global.Lang);
                            Model.Local.Attachment            Attachment       = new Model.Local.Attachment()
                            {
                                Att_FileName    = PsAttachment.FileName,
                                Att_Description = PsAttachmentLang.Description,
                                Att_Mime        = PsAttachment.Mime,
                                Att_Name        = PsAttachmentLang.Name,
                                Att_File        = this.ReadFile(DirAttachment, PsAttachment.File),
                                Pre_Id          = Convert.ToInt32(PsAttachment.IDAttachment),
                                Art_Id          = Article.Art_Id
                            };
                            AttachmentRepository.Add(Attachment);

                            String FTP      = Core.Global.GetConfig().ConfigFTPIP;
                            String User     = Core.Global.GetConfig().ConfigFTPUser;
                            String Password = Core.Global.GetConfig().ConfigFTPPassword;

                            // <JG> 21/05/2013 correction recherche fichier sur le ftp
                            string ftpfullpath           = FTP + "/download/" + PsAttachment.File;
                            System.Net.FtpWebRequest ftp = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(ftpfullpath);
                            ftp.Credentials = new System.Net.NetworkCredential(User, Password);
                            ftp.UseBinary   = true;
                            ftp.UsePassive  = true;
                            ftp.KeepAlive   = false;
                            ftp.EnableSsl   = Core.Global.GetConfig().ConfigFTPSSL;

                            System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse();
                            Stream reader = response.GetResponseStream();

                            MemoryStream memStream      = new MemoryStream();
                            byte[]       buffer         = new byte[1024];
                            byte[]       downloadedData = new byte[0];
                            while (true)
                            {
                                int bytesRead = reader.Read(buffer, 0, buffer.Length);
                                if (bytesRead != 0)
                                {
                                    memStream.Write(buffer, 0, bytesRead);
                                }
                                else
                                {
                                    break;
                                }
                                downloadedData = memStream.ToArray();
                            }

                            if (downloadedData != null && downloadedData.Length != 0)
                            {
                                FileStream newFile = new FileStream(DirAttachment + Attachment.Att_File, FileMode.Create);
                                newFile.Write(downloadedData, 0, downloadedData.Length);
                                newFile.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Error.SendMailError(ex.ToString());
            }
        }
Exemplo n.º 8
0
        string err_msg_str;     // error発生時の内容

        /**
         * @brief       OneFile ftp download
         * @param[in]   string  name    login name
         * @param[in]   string  pass    password
         * @param[in]   string  uri     DownloadするFileの URI 例"ftp://localhost/test_e.exe"
         * @param[in]   string  local_path     :DownloadしたFileの保存先 例"C:\\test_e.exe"
         * @return      bool    true:ok  false:error
         */
        public bool FtpOneFileDown(string name, string pass, string uri, string local_path)
        {
            Uri    uri_obj      = new Uri(uri);
            string downFilePath = local_path;

            err_msg_str = "no info.";

            // FtpWebRequestの作成
            System.Net.FtpWebRequest ftp_req = (System.Net.FtpWebRequest)
                                               System.Net.WebRequest.Create(uri_obj);

            // ログインユーザー名とパスワードを設定
            ftp_req.Credentials = new System.Net.NetworkCredential(name, pass);

            // MethodにWebRequestMethods.Ftp.DownloadFile("RETR")を設定
            ftp_req.Method     = System.Net.WebRequestMethods.Ftp.DownloadFile;
            ftp_req.KeepAlive  = false; // 要求の完了後に接続を閉じる
            ftp_req.UseBinary  = false; // Binaryモードで転送する
            ftp_req.UsePassive = true;  // PASSIVEモードを有効にする

            bool ok_f = true;

            System.Net.FtpWebResponse ftp_res  = null;
            System.IO.Stream          res_strm = null;
            try
            {
                // FtpWebResponseを取得
                ftp_res = (System.Net.FtpWebResponse)ftp_req.GetResponse();

                // ファイルをダウンロードするためのStreamを取得
                res_strm = ftp_res.GetResponseStream();
            }
            catch (System.Net.WebException e_msg)
            {
                ok_f        = false;
                err_msg_str = e_msg.Message;

                if (ftp_res != null)
                {
                    ftp_res.Close();
                }
                if (res_strm != null)
                {
                    res_strm.Close();
                }
            }

            if (ok_f == true)
            {
                // ダウンロードしたファイルを書き込むためのFileStreamを作成
                System.IO.FileStream fs = null;
                fs = new System.IO.FileStream(downFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                try
                {
                    // ダウンロードしたデータを書き込む
                    int    size   = 1024;
                    byte[] buffer = new byte[size];
                    while (true)
                    {
                        int readSize = res_strm.Read(buffer, 0, buffer.Length);
                        if (readSize == 0)
                        {
                            break;
                        }
                        fs.Write(buffer, 0, readSize);
                    }

                    // FTPサーバーから送信されたステータスを表示
                    //Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription);
                }
                catch (System.IO.IOException ie)
                {
                    Console.WriteLine("", ie.Message);
                }
                finally {
                    if (fs != null)
                    {
                        fs.Close();
                    }

                    if (res_strm != null)
                    {
                        res_strm.Close();
                    }

                    if (ftp_res != null)
                    {
                        ftp_res.Close();
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }