예제 #1
0
        private List <FileStruct> GetList(string datastring)
        {
            List <FileStruct> myListArray = new List <FileStruct>();

            string[] dataRecords = datastring.Split('\n');
            //Получаем стиль записей на сервере
            FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);

            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    FileStruct f = new FileStruct();
                    f.Name = "..";
                    switch (_directoryListStyle)
                    {
                    case FileListStyle.UnixStyle:
                        f = ParseFileStructFromUnixStyleRecord(s);
                        break;

                    case FileListStyle.WindowsStyle:
                        f = ParseFileStructFromWindowsStyleRecord(s);
                        break;
                    }
                    myListArray.Add(f);

                    /*if (f.Name != "" && f.Name != ".")// && f.Name != "..")
                     * {
                     * }*/
                }
            }
            return(myListArray);
        }
예제 #2
0
        /// <summary>
        /// 获得目录和文件列表
        /// </summary>
        /// <param name="path">上级目录</param>
        public List <FileDirectoryInfo> GetFileFolders(string path)
        {
            List <FileDirectoryInfo> directorys = new List <FileDirectoryInfo>();

            FtpWebRequest reqFTP = CreateFtpConnection(path);

            reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            reqFTP.KeepAlive = false;
            WebResponse response = null;

            try
            {
                response = reqFTP.GetResponse();
            }
            catch
            {
                //patch:当前连接或最近一次连接的“KeepAlive=false”时,ftp可能返回501错误。出错后再尝试一次。
                reqFTP           = CreateFtpConnection(path);
                reqFTP.Method    = WebRequestMethods.Ftp.ListDirectoryDetails;
                reqFTP.KeepAlive = false;
                response         = reqFTP.GetResponse();
            }

            Encoding encoding = Encoding.Default;

            if (this._decodeType == DecodeType.UTF8)
            {
                encoding = Encoding.UTF8;
            }

            StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
            string       line   = reader.ReadLine();

            FileListStyle fileListStyle = FileListStyle.Unknown;

            if (line != null)
            {
                fileListStyle = GetFileListStyle(line);
            }

            while (line != null)
            {
                fileListStyle = GetFileListStyle(line);
                FileDirectoryInfo temp = ConvertToDirectory(line, fileListStyle);
                if (temp != null)
                {
                    if (!temp.DisplayName.StartsWith("."))
                    {
                        directorys.Add(temp);
                    }
                }
                line = reader.ReadLine();
            }

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

            return(directorys);
        }
예제 #3
0
        /// <summary>
        /// 获得文件和目录列表
        /// </summary>
        /// <param name="datastring">FTP返回的列表字符信息</param>
        private FtpFileItem[] GetList(string[] dataRecords)
        {
            List <FtpFileItem> myListArray         = new List <FtpFileItem>();
            FileListStyle      _directoryListStyle = GuessFileListStyle(dataRecords);

            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    FtpFileItem f = new FtpFileItem();
                    f.Name = "..";
                    switch (_directoryListStyle)
                    {
                    case FileListStyle.UnixStyle:
                        f = ParseFileStructFromUnixStyleRecord(s);
                        break;

                    case FileListStyle.WindowsStyle:
                        f = ParseFileStructFromWindowsStyleRecord(s);
                        break;
                    }
                    if (!(f.Name == "." || f.Name == ".."))
                    {
                        myListArray.Add(f);
                    }
                }
            }
            myListArray.Sort(delegate(FtpFileItem x, FtpFileItem y)
            {
                return(x.CompareTo(y));
            });

            return(myListArray.ToArray());
        }
예제 #4
0
        /// <summary>
        /// 转换获取到的目录或者文件为对应的对象列表
        /// </summary>
        /// <param name="dataRecords">目录或者文件的字符串</param>
        /// <returns></returns>
        private List <FileStruct> ConvertStruct(List <string> dataRecords)
        {
            List <FileStruct> list      = new List <FileStruct>();
            FileListStyle     listStyle = GuessFileListStyle(dataRecords);

            foreach (string s in dataRecords)
            {
                if (listStyle != FileListStyle.Unknown && s != "")
                {
                    FileStruct f = new FileStruct();
                    f.Name = "..";
                    switch (listStyle)
                    {
                    case FileListStyle.UnixStyle:
                        f = ParseFileStructFromUnixStyle(s);
                        break;

                    case FileListStyle.WindowsStyle:
                        f = ParseFileStructFromWindowsStyle(s);
                        break;
                    }
                    if (!(f.Name == "." || f.Name == ".."))
                    {
                        list.Add(f);
                    }
                }
            }
            return(list);
        }
예제 #5
0
        private FileStruct[] method_6(string string_5)
        {
            List <FileStruct> list = new List <FileStruct>();

            string[]      strArray = string_5.Split(new char[] { '\n' });
            FileListStyle style    = this.method_8(strArray);

            foreach (string str in strArray)
            {
                if ((style == FileListStyle.Unknown) || !(str != ""))
                {
                    continue;
                }
                FileStruct item = new FileStruct {
                    Name = ".."
                };
                switch (style)
                {
                case FileListStyle.UnixStyle:
                    item = this.method_9(str);
                    break;

                case FileListStyle.WindowsStyle:
                    item = this.method_7(str);
                    break;
                }
                if ((item.Name != ".") && (item.Name != ".."))
                {
                    list.Add(item);
                }
            }
            return(list.ToArray());
        }
예제 #6
0
        /// <summary>
        /// 获得文件和目录列表
        /// </summary>
        /// <param name="datastring">FTP返回的列表字符信息</param>
        private FileStruct[] GetList(string datastring)
        {
            List <FileStruct> myListArray = new List <FileStruct>();

            string[]      dataRecords         = datastring.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);

            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    FileStruct f = new FileStruct();
                    f.Name = "..";
                    switch (_directoryListStyle)
                    {
                    case FileListStyle.UnixStyle:
                        f = ParseFileStructFromUnixStyleRecord(s);
                        break;

                    case FileListStyle.WindowsStyle:
                        f = ParseFileStructFromWindowsStyleRecord(s);
                        break;
                    }
                    if (!(f.Name == "." || f.Name == ".." || f.Name == String.Empty))
                    {
                        myListArray.Add(f);
                    }
                }
            }
            return(myListArray.ToArray());
        }
예제 #7
0
        private FileStruct[] GetList(string datastring)
        {
            List <FileStruct> list = new List <FileStruct>();

            string[]      recordList = datastring.Split(new char[] { '\n' });
            FileListStyle style      = this.GuessFileListStyle(recordList);

            foreach (string str in recordList)
            {
                if ((style == FileListStyle.Unknown) || !(str != ""))
                {
                    continue;
                }
                FileStruct item = new FileStruct {
                    Name = ".."
                };
                switch (style)
                {
                case FileListStyle.UnixStyle:
                    item = this.ParseFileStructFromUnixStyleRecord(str);
                    break;

                case FileListStyle.WindowsStyle:
                    item = this.ParseFileStructFromWindowsStyleRecord(str);
                    break;
                }
                if ((item.Name != ".") && (item.Name != ".."))
                {
                    list.Add(item);
                }
            }
            return(list.ToArray());
        }
    private List <FileStruct> GetList(string datastring)
    {
        List <FileStruct> myListArray = new List <FileStruct>();

        string[]      dataRecords         = datastring.Split('\n');
        FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);

        foreach (string s in dataRecords)
        {
            if (_directoryListStyle != FileListStyle.Unknown && s != "")
            {
                FileStruct f = new FileStruct();
                f.Name = "..";
                switch (_directoryListStyle)
                {
                case FileListStyle.UnixStyle:
                    f = ParseFileStructFromUnixStyleRecord(s);
                    break;

                case FileListStyle.WindowsStyle:
                    f = ParseFileStructFromWindowsStyleRecord(s);
                    break;
                }
                if (!(f.Name == "." || f.Name == ".."))
                {
                    myListArray.Add(f);
                }
            }
        }

        return(myListArray);;
    }
예제 #9
0
        /// <summary>
        /// 获取当前目录下的目录及文件
        /// </summary>
        /// param name="ftpfileList"></param>
        /// <param name="DirectoryPath"></param>
        /// <returns></returns>
        public List <FileStruct> GetFtpFile(string DirectoryPath, int ilevel = 0)
        {
            //var ftpfileList = new List<ActFile>();
            List <FileStruct> myListArray = new List <FileStruct>();
            string            uri         = Path.Combine(ftpURI, DirectoryPath);

            return(MethodInvoke(String.Format(@"GetFtpFile({0})", DirectoryPath), () =>
            {
                FtpWebRequest ftp = GetRequest(uri, WebRequestMethods.Ftp.ListDirectoryDetails);
                //ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                string Datastring = string.Empty;
                //StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//中文文件名
                using (Stream st = ftp.GetResponse().GetResponseStream())
                {
                    using (StreamReader stream = new StreamReader(st, System.Text.Encoding.UTF8)) //
                    {
                        Datastring = stream.ReadToEnd();
                        //stream = null;
                        //st = null;
                        //stream.Close();
                        //stream.Dispose();
                    }
                    //st.Close();
                    //st.Dispose();
                }
                string[] dataRecords = Datastring.Split('\n');
                FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);
                foreach (string s in dataRecords)
                {
                    if (_directoryListStyle != FileListStyle.Unknown && s != "")
                    {
                        FileStruct f = new FileStruct();
                        f.Name = "..";
                        switch (_directoryListStyle)
                        {
                        case FileListStyle.UnixStyle:
                            f = ParseFileStructFromUnixStyleRecord(s, DirectoryPath, ilevel);
                            break;

                        case FileListStyle.WindowsStyle:
                            f = ParseFileStructFromWindowsStyleRecord(s, DirectoryPath, ilevel);
                            break;
                        }
                        if (!(f.Name == "." || f.Name == ".."))
                        {
                            myListArray.Add(f);
                        }
                    }
                }
                return myListArray;
            }));
        }
예제 #10
0
파일: Network.cs 프로젝트: wsw621012/Toys
        static FileListStyle GetFileListStyle(string str)
        {
            FileListStyle style = FileListStyle.Unknown;

            if (Regex.IsMatch(str.Substring(0, 10), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)"))
            {
                style = FileListStyle.UnixStyle;
            }
            else if (Regex.IsMatch(str.Substring(0, 8), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]"))
            {
                style = FileListStyle.WindowsStyle;
            }
            return(style);
        }
예제 #11
0
        /// <summary>
        /// Parses list of strings to list of FileStruct
        /// </summary>
        /// <param name="ftpRecords">String including information of file/directory</param>
        /// <returns>List of FileStruct</returns>
        public List <FileStruct> Parse(List <string> ftpRecords)
        {
            List <FileStruct> myListArray = new List <FileStruct>();
            //string[] dataRecords = datastring.Split( '\n' );
            //dataRecords[ 0 ] = "-rw-rw-rw- 1 user group 1171 Nov 26 00:43 blue.css\n";
            FileListStyle _directoryListStyle = GuessFileListStyle(ftpRecords);

            foreach (string s in ftpRecords)
            {
                FileStruct f = Parse(s);
                if (!(f.Name == "." || f.Name == ".."))
                {
                    myListArray.Add(f);
                }
            }
            return(myListArray);
        }// method
예제 #12
0
        /// <summary>
        /// 判断文件列表的方式Window方式还是Unix方式
        /// </summary>
        public void InitializeFileListStyle()
        {
            var    listDirectoryDetails = FindFilesAndDirectories(_ftpUri, WebRequestMethods.Ftp.ListDirectoryDetails).FileList;
            string line = listDirectoryDetails.FirstOrDefault(x => x.Length > 8) ?? "";

            if (line.Length > 10 && Regex.IsMatch(line.Substring(0, 10), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)"))
            {
                _fileListStyle = FileListStyle.UnixStyle;
            }
            else if (line.Length > 8 && Regex.IsMatch(line.Substring(0, 8), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]"))
            {
                _fileListStyle = FileListStyle.WindowsStyle;
            }
            else
            {
                _fileListStyle = FileListStyle.Unknown;
            }
        }
예제 #13
0
        }// method

        /// <summary>
        /// Parses string to FileStruct
        /// </summary>
        /// <param name="ftpRecord">String including information of file/directory</param>
        /// <returns>FileStruct</returns>
        public FileStruct Parse(string ftpRecord)
        {
            FileStruct    f = new FileStruct();
            FileListStyle _directoryListStyle = GuessFileListStyle(ftpRecord);

            if (_directoryListStyle != FileListStyle.Unknown && ftpRecord != "")
            {
                f.Name = "..";
                switch (_directoryListStyle)
                {
                case FileListStyle.UnixStyle:
                    f = ParseFileStructFromUnixStyleRecord(ftpRecord);
                    break;

                case FileListStyle.WindowsStyle:
                    f = ParseFileStructFromWindowsStyleRecord(ftpRecord);
                    break;
                } //switch
            }     //if
            return(f);
        }         //
예제 #14
0
        /// <summary>
        /// Создание коллеции из полученных файлов
        /// </summary>
        /// <param name="data">данные с сервера</param>
        /// <returns>Коллекция данных</returns>
        private ObservableCollection <FileInformation> GetList(string data)
        {
            ObservableCollection <FileInformation> fileList = new ObservableCollection <FileInformation> ( );

            string [] dataRecords = data.Split('\n');

            FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);

            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    FileInformation temp = new FileInformation {
                        ShortName = ".",
                        FullName  = "."
                    };

                    switch (_directoryListStyle)
                    {
                    case FileListStyle.UnixStyle:
                        temp = ParseFileStructFromUnixStyleRecord(s);
                        break;

                    case FileListStyle.WindowsStyle:
                        temp = ParseFileStructFromWindowsStyleRecord(s);
                        break;
                    }

                    if (temp.FullName != "" && temp.FullName != "." && temp.FullName != "..")
                    {
                        temp.ConvertName( );
                        fileList.Add(temp);
                    }
                }
            }
            return(fileList);
        }
예제 #15
0
        /// <summary>
        /// 获得文件和目录列表
        /// </summary>
        /// <paramKey colName="datastring">FTP返回的列表字符信息</paramKey>
        /// <paramKey colName="relPath">相对路径</paramKey>
        private FileStruct[] GetList(string datastring, string relPath)
        {
            List <FileStruct> myListArray = new List <FileStruct>();

            string[] dataRecords = datastring.Split('\n');
            _directoryListStyle = GuessFileListStyle(dataRecords);
            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    try
                    {
                        FileStruct f = new FileStruct();
                        f.Name = "..";
                        switch (_directoryListStyle)
                        {
                        case FileListStyle.UnixStyle:

                            f = ParseFileStructFromUnixStyleRecord(s, relPath);

                            break;

                        case FileListStyle.WindowsStyle:
                            f = ParseFileStructFromWindowsStyleRecord(s, relPath);
                            break;
                        }
                        if (!(f.Name == "." || f.Name == ".."))
                        {
                            myListArray.Add(f);
                        }
                    }
                    catch { } //默认第一行,不管
                }
            }
            return(myListArray.ToArray());
        }
예제 #16
0
        /// <summary>
        /// 获得文件和目录列表
        /// </summary>
        /// <param name="datastring">FTP返回的列表字符信息</param>
        private FileStruct[] GetList(string datastring)
        {
            List <FileStruct> myListArray = new List <FileStruct>();

            string[]      tmpdataRecords      = datastring.Split('\n');
            FileListStyle _directoryListStyle = GuessFileListStyle(tmpdataRecords);
            List <string> dataRecords         = tmpdataRecords.ToList();

            if (dataRecords[0].IndexOf("total") >= 0)
            {
                dataRecords.RemoveAt(0);
            }
            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    FileStruct f = new FileStruct();
                    f.Name = "..";
                    switch (_directoryListStyle)
                    {
                    case FileListStyle.UnixStyle:
                        f = ParseFileStructFromUnixStyleRecord(s);
                        break;

                    case FileListStyle.WindowsStyle:
                        f = ParseFileStructFromWindowsStyleRecord(s);
                        break;
                    }
                    if (!(f.Name == "." || f.Name == ".."))
                    {
                        myListArray.Add(f);
                    }
                }
            }
            return(myListArray.ToArray());
        }
예제 #17
0
파일: Network.cs 프로젝트: wsw621012/Toys
        public static string GetLatestFileInDirectory(FtpWebRequest request)
        {
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            DateTime latest_date_time = new DateTime();
            string   latest_file      = null;

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                StreamReader  reader = new StreamReader(response.GetResponseStream());
                FileListStyle style  = FileListStyle.Unknown;
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (style == FileListStyle.Unknown)
                    {
                        style = GetFileListStyle(line);
                    }
                    string date_time_str = null;
                    string file_name     = null;
                    if (style == FileListStyle.WindowsStyle)
                    {
                        Match match = Regex.Match(line, @"^(?<date_time>.+M).+\s+(?<file_name>\S+)$");
                        if (match.Success)
                        {
                            date_time_str = match.Groups["date_time"].Value;
                            file_name     = match.Groups["file_name"].Value;
                        }
                    }
                    else
                    {
                        Match match = Regex.Match(line, @".+\s(?<date_time>\S+\s+\S+\s+\S+)\s(?<file_name>\S+)$");
                        if (match.Success)
                        {
                            date_time_str = match.Groups["date_time"].Value;
                            file_name     = match.Groups["file_name"].Value;
                            Match match2 = Regex.Match(date_time_str, @"^(?<month_day>\S+\s+\S+)\s+(?<time>\d+:\d+)");
                            if (match2.Success)
                            {
                                string   month_day  = match2.Groups["month_day"].Value;
                                string   time       = match2.Groups["time"].Value;
                                DateTime now        = DateTime.Now;
                                DateTime next_month = now.AddMonths(1);
                                date_time_str = string.Format("{0} {1} {2}", month_day, now.Year, time);
                                DateTime temp_dt = DateTime.Parse(date_time_str);
                                if (temp_dt > next_month)
                                {
                                    date_time_str = string.Format("{0} {1} {2}", month_day, now.Year - 1, time);
                                }
                            }
                        }
                    }
                    DateTime date_time = DateTime.Parse(date_time_str, new CultureInfo("en-us", true));
                    if (date_time > latest_date_time)
                    {
                        latest_date_time = date_time;
                        latest_file      = file_name;
                    }
                }
            }
            return(latest_file);
        }
예제 #18
0
        /// <summary>
        /// 获得文件和目录列表
        /// </summary>
        /// <paramKey colName="datastring">FTP返回的列表字符信息</paramKey>
        /// <paramKey colName="relPath">相对路径</paramKey>
        private FileStruct[] GetList(string datastring, string relPath)
        {
            List<FileStruct> myListArray = new List<FileStruct>();
            string[] dataRecords = datastring.Split('\n');
            _directoryListStyle = GuessFileListStyle(dataRecords);
            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    try
                    {
                        FileStruct f = new FileStruct();
                        f.Name = "..";
                        switch (_directoryListStyle)
                        {
                            case FileListStyle.UnixStyle:

                                f = ParseFileStructFromUnixStyleRecord(s, relPath);

                                break;
                            case FileListStyle.WindowsStyle:
                                f = ParseFileStructFromWindowsStyleRecord(s, relPath);
                                break;
                        }
                        if (!(f.Name == "." || f.Name == ".."))
                        {
                            myListArray.Add(f);
                        }
                    }
                    catch { } //默认第一行,不管
                }
            }
            return myListArray.ToArray();
        }
예제 #19
0
        /// <summary>
        /// 将文件详细信息字符串转换为文件类
        /// </summary>
        private FileDirectoryInfo ConvertToDirectory(string fileName, FileListStyle style)
        {
            Match m = GetMatchingRegex(fileName);

            if (m == null)
            {
                return(null);
            }

            FileDirectoryInfo result = new FileDirectoryInfo();

            result.DisplayName = m.Groups["name"].Value;
            if (m.Groups["size"].Value != string.Empty)
            {
                result.FileSize = long.Parse(m.Groups["size"].Value);
            }
            if (m.Groups["timestamp"].Value != string.Empty)
            {
                DateTimeFormatInfo formatProvider = new CultureInfo("en-US", false).DateTimeFormat;

                try
                {
                    string dateStr = m.Groups["timestamp"].Value;
                    switch (style)
                    {
                    case FileListStyle.UnixStyle:
                        //example1:“Jan 18  2009”;example2:“Jan 11 12:29”
                        if (dateStr.IndexOf(":") != -1)
                        {
                            dateStr = string.Format("{0} {1}", DateTime.Now.Year, dateStr);
                        }

                        result.LastEditTime = DateTime.Parse(dateStr, formatProvider);
                        //距离当前时间(以文件服务器时间为参考点)半年以内的时间不带年份
                        if (result.LastEditTime.Month > DateTime.Now.Month)
                        {
                            result.LastEditTime = result.LastEditTime.AddYears(-1);
                        }
                        break;

                    case FileListStyle.WindowsStyle:
                    //example3:“01-11-11  06:23PM”
                    case FileListStyle.Unknown:
                    default:
                        result.LastEditTime = DateTime.Parse(dateStr, formatProvider);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(string.Format("获取文件修改时间出错[{0}]", fileName), ex);
                }
            }
            string _dir = m.Groups["dir"].Value;

            if (_dir != "" && _dir != "-")
            {
                result.FileInfoType = DirectoryEntryTypes.目录;
            }
            else
            {
                result.FileInfoType = DirectoryEntryTypes.文件;
            }

            return(result);
        }