Пример #1
0
        /// <summary>
        ///     ''' Scarica file da server ftp
        ///     ''' </summary>
        ///     ''' <param name="url"></param>
        ///     ''' <param name="localFile"></param>
        ///     ''' <param name="user"></param>
        ///     ''' <param name="pass"></param>
        ///     ''' <remarks></remarks>
        public static void DownloadFile(string url, string localFile, string user, string pass)
        {
            FtpWebRequest reqFtp = Ftp.CreateFtpRequest(url, user, pass, false);

            reqFtp.Method    = WebRequestMethods.Ftp.DownloadFile;
            reqFtp.UseBinary = true;
            FtpWebResponse respFtp = (FtpWebResponse)reqFtp.GetResponse();

            using (System.IO.Stream respFtpStream = respFtp.GetResponseStream())
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(localFile, System.IO.FileMode.Create))
                {
                    byte[] buffer = new byte[Int16.MaxValue + 1];
                    int    rBytes = respFtpStream.Read(buffer, 0, Int16.MaxValue);
                    while (rBytes > 0)
                    {
                        // Scrive su file
                        fs.Write(buffer, 0, rBytes);

                        // Legge
                        rBytes = respFtpStream.Read(buffer, 0, Int16.MaxValue);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     ''' Elimina la cartella e tutto il suo contenuto (sottocartelle comprese)
        ///     ''' </summary>
        ///     ''' <param name="url"></param>
        ///     ''' <param name="user"></param>
        ///     ''' <param name="pass"></param>
        ///     ''' <remarks></remarks>
        public static void DeleteFolderTree(string url, string user, string pass)
        {
            string[] listaFtp = Ftp.GetList(url, user, pass);
            string   newFtpUrl;

            foreach (string sEntry in listaFtp)
            {
                // Calcola nuovo url (sia file che dir)
                newFtpUrl = string.Format("{0}/{1}", url, System.IO.Path.GetFileName(sEntry));

                if (sEntry.Contains("."))
                {
                    // Elimia File
                    Ftp.DeleteFile(newFtpUrl, user, pass);
                }
                else
                {
                    // Elimina Directory Tree
                    Ftp.DeleteFolderTree(newFtpUrl, user, pass);
                }
            }

            // Elimina cartella vuota
            Ftp.DeleteEmptyFolder(url, user, pass);
        }
Пример #3
0
        /// <summary>
        ///     ''' Elimina file singolo
        ///     ''' </summary>
        ///     ''' <param name="url"></param>
        ///     ''' <param name="user"></param>
        ///     ''' <param name="pass"></param>
        ///     ''' <remarks></remarks>
        public static void DeleteFile(string url, string user, string pass)
        {
            FtpWebRequest reqFtp = Ftp.CreateFtpRequest(url, user, pass, false);

            reqFtp.Method    = WebRequestMethods.Ftp.DeleteFile;
            reqFtp.UseBinary = true;
            FtpWebResponse respFtp = (FtpWebResponse)reqFtp.GetResponse();

            respFtp.Close();
        }
Пример #4
0
        /// <summary>
        ///     ''' Elimina una cartella VUOTA
        ///     ''' </summary>
        ///     ''' <param name="url"></param>
        ///     ''' <param name="user"></param>
        ///     ''' <param name="pass"></param>
        ///     ''' <remarks></remarks>
        public static void DeleteEmptyFolder(string url, string user, string pass)
        {
            FtpWebRequest reqFtp = Ftp.CreateFtpRequest(url, user, pass, false);

            reqFtp.Method    = WebRequestMethods.Ftp.RemoveDirectory;
            reqFtp.UseBinary = true;
            FtpWebResponse respFtp = (FtpWebResponse)reqFtp.GetResponse();

            respFtp.Close();
        }
Пример #5
0
        /// <summary>
        ///     ''' Esegue Upload File
        ///     ''' </summary>
        ///     ''' <param name="url"></param>
        ///     ''' <param name="localFile"></param>
        ///     ''' <param name="user"></param>
        ///     ''' <param name="pass"></param>
        ///     ''' <remarks></remarks>
        public static void UploadFile(string url, string localFile, string user, string pass)
        {
            FtpWebRequest reqFtp = Ftp.CreateFtpRequest(url, user, pass, false);

            reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
            using (System.IO.Stream ftpStream = reqFtp.GetRequestStream())
            {
                byte[] buff = System.IO.File.ReadAllBytes(localFile);
                ftpStream.Write(buff, 0, buff.Length);
            }
        }
Пример #6
0
        /// <summary>
        ///     ''' Elenco contenuto directory
        ///     ''' </summary>
        ///     ''' <param name="url"></param>
        ///     ''' <param name="user"></param>
        ///     ''' <param name="pass"></param>
        ///     ''' <remarks></remarks>
        public static string[] GetList(string url, string user, string pass)
        {
            FtpWebRequest reqFtp = Ftp.CreateFtpRequest(url, user, pass, false);

            reqFtp.Method    = WebRequestMethods.Ftp.ListDirectory;
            reqFtp.UseBinary = true;

            FtpWebResponse respFtp = (FtpWebResponse)reqFtp.GetResponse();
            string         retStr  = string.Empty;

            using (System.IO.StreamReader ftpStremReader = new System.IO.StreamReader(respFtp.GetResponseStream()))
            {
                retStr = ftpStremReader.ReadToEnd();
            }

            return(retStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
        }