如何遍历一个ftp目录word生成索引目录文件

C#如何实现FTP在服务器上遍历文件夹_最权威经验分享,100%已解决!
后使用快捷导航没有帐号?
天府交友网向您保证:
总有一天你会在这里结识到理想的朋友、恋人、商友或者知己!
只需一步,快速开始
文章地址:
文章摘要:
using S namespace test { class Program { public static void Main(string[] args) { //改成FTP服务器地址 //string dir = @\\\\192.168.1.106\\eux\\ ; string dir = @F:\\eux\\ ; print(dir); Console.Write(Press any key to continue . . , C#如何多次连接数据库啊? . ); Console.
时间: 22:36来源:未知 作者:admin 点击:
using S namespace test { class Program { public static void Main(string[] args) { //改成FTP服务器地址 //string dir = @&\\\\192.168.1.106\\eux\\& ; string dir = @&F:\\eux\\& ; print(dir); Console.Write(&Press any key to continue . . ,. &); Console.ReadKey(true); } public static void print(string dir) { if(System.IO.Directory.Exists(dir))
string []AllFiles = System,.IO,.Directory.GetFiles(dir);
foreach(string fName in AllFiles)
if(System.IO.File,.Exists(fName))
Console.WriteLine(&FileName:{0}&,fName);
string []AllDirectories = System.IO,.Directory.GetDirectories(dir); foreach(string dirName in AllDirectories) {
if(System.IO.Directory,.Exists(dirName))
Console.WriteLine(&DirectoryName:{0}&,dirName);
print(dirName);
(责任编辑:admin)FTP获取根目录下的所有子文件
FTP获取根目录下的所有子文件
/**&* FTPUtil.java&* xujy &* @version 0.1&*/package com.miaxis.www.util.import java.io.DataInputSimport java.io.Fimport java.io.IOEimport java.net.SocketEimport java.util.ArrayLimport java.util.Limport mons.net.ftp.FTP;import mons.net.ftp.FTPCimport mons.net.ftp.FTPClientCimport mons.net.ftp.FTPFimport mons.net.ftp.FTPRimport org.apache.log4j.Lpublic class FTPUtil1 {& & private static Logger logger = Logger.getLogger(FTPUtil1.class);& & FTPClient ftpClient =& & private String path = "";&& & /**& & &* 获取FTPClient对象& & &*&& & &* @param ftpHost& & &* & & & & & &FTP主机服务器& & &* @param ftpPassword& & &* & & & & & &FTP 登录密码& & &* @param ftpUserName& & &* & & & & & &FTP登录用户名& & &* @param ftpPort& & &* & & & & & &FTP端口 默认为21& & &* @return& & &*/& & public &FTPClient getFTPClient(String ftpHost, String ftpPassword,& & & & & & String ftpUserName,int port) {& & & & try {& & & & & & ftpClient = new FTPClient();& & & & & && & & & & & ftpClient.setDefaultPort(port); //设置默认端口& & & & & & ftpClient.configure(getFtpConfig());&& & & & & & ftpClient.connect(ftpHost);// 连接FTP服务器& & & & & & ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();&//
ftpClient.setConnectTimeout(60000);& & & & & & boolean isSuccess = ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器& & & & & & if(isSuccess){& & & & & & & & ("FTP登录成功。");& & & & & & }& & & & & & if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {& & & & & & & & ("未连接到FTP,用户名或密码错误。");& & & & & & & & ftpClient.disconnect();& & & & & & } else {& & & & & & & & ("FTP连接成功。");& & & & & & }& & & & } catch (SocketException e) {& & & & & & ("FTP的IP地址可能错误,请正确配置。");& & & & & & e.printStackTrace();& & & & } catch (IOException e) {& & & & & & ("FTP的端口错误,请正确配置。");& & & & & & e.printStackTrace();& & & & }& & & & return ftpC& & }& & &//设置FTP客服端的配置--一般可以不设置&& & private & FTPClientConfig getFtpConfig() {&& & & & & & FTPClientConfig ftpConfig = new FTPClientConfig(FTPClientConfig.SYST_UNIX);&& & & & & & ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);&& & & & & & return ftpC&& & }&& & public static void &recursion(String root){& &
&File file = new File(root);& &
&File[] subFile = file.listFiles();& &
&for (int i = 0; i & subFile. i++) {& &
& if (subFile[i].isDirectory()) {& &
& &System.out.println("目录: " + subFile[i].getName());& &
& &recursion(subFile[i].getAbsolutePath());& &
& }else{& &
& &System.out.println("文件: " + subFile[i].getName());& &
}& & /**&& & &* 列出Ftp服务器上的所有文件和目录&& & &*/&& & public &void listRemoteAllFiles(String remotePath) {&& &
FTPUtil1 util=new FTPUtil1();& &
ftpClient = util.getFTPClient("ftp服务的IP地址", "用户名", "密码",21);//21是端口,根据自己情况而定& & & & try {&// & & & & & & & &String[] names = ftpClient.listNames();&& & & & & & & & FTPFile[] files = ftpClient.listFiles(remotePath);&& & & & & & & & for (int i = 0; i & files. i++) { &&& & & & & & & &
if (files[i].isFile()) { &&& & & & & & & &
System.out.println(files[i].getName()); &&& & & & & & & &
} else if (files[i].isDirectory()) {&& & & & & & & &
System.out.println("目录"+files[i].getName());& & & & & & & &
listRemoteAllFiles(remotePath + files[i].getName() + "/");&& & & & & & & &
System.out.println("遍历结束");& & & & & & & & & & }& & & & & & & & }& & & & } catch (Exception e) {&& & & & & & & & e.printStackTrace();&& & & & }&& & }& public static void main(String args[]){
FTPUtil1 util=new FTPUtil1();
util.listRemoteAllFiles("/");& & }& &&}
发表评论:
TA的最新馆藏ftp下载目录下所有文件及文件夹内(递归)
/// &summary&&&
&& /// ftp文件上传、下载操作类&&
&& /// &/summary&&&
&& public class FTPHelper&
&&&&&& /// &summary&&&
&&&&&& /// ftp用户名,匿名为&&&&
&&&&&& /// &/summary&&&
&&&&&& private string ftpU&
&&&&&& /// &summary&&&
&&&&&& /// ftp用户密码,匿名为&&&&
&&&&&& /// &/summary&&&
&&&&&& private string ftpPassW&
&&&&&& /// &summary&&&
&&&&&& ///通过用户名,密码连接到FTP服务器&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&ftpUser&&ftp用户名,匿名为&&&/param&&&
&&&&&& /// &param name=&ftpPassWord&&ftp登陆密码,匿名为&&&/param&&&
&&&&&& public FTPHelper(string ftpUser, string ftpPassWord)&
&&&&&&&&&& this.ftpUser = ftpU&
&&&&&&&&&& this.ftpPassWord = ftpPassW&
&&&&&& /// &summary&&&
&&&&&& /// 匿名访问&&
&&&&&& /// &/summary&&&
&&&&&& public FTPHelper()&
&&&&&&&&&& this.ftpUser = &&;&
&&&&&&&&&& this.ftpPassWord = &&;&
&&&&&& /// &summary&&&
&&&&&& /// 上传文件到Ftp服务器&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&uri&&把上传的文件保存为ftp服务器文件的uri,如&ftp://192.168.1.104/capture-212.avi&&/param&&&
&&&&&& /// &param name=&upLoadFile&&要上传的本地的文件路径,如D:\capture-2.avi&/param&&&
&&&&&& public void UpLoadFile(string UpLoadUri, string upLoadFile)&
&&&&&&&&&& Stream requestStream =&
&&&&&&&&&& FileStream fileStream =&
&&&&&&&&&& FtpWebResponse uploadResponse =&
&&&&&&&&&& try&
&&&&&&&&&& {&
&&&&&&&&&&&&&& Uri uri = new Uri(UpLoadUri);&
&&&&&&&&&&&&&& FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);&
&&&&&&&&&&&&&& uploadRequest.Method = WebRequestMethods.Ftp.UploadF&
&&&&&&&&&&&&&& uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);&
&&&&&&&&&&&&&& requestStream = uploadRequest.GetRequestStream();&
&&&&&&&&&&&&&& fileStream = File.Open(upLoadFile, FileMode.Open);&
&&&&&&&&&&&&&& byte[] buffer = new byte[1024];&
&&&&&&&&&&&&&& int bytesR&
&&&&&&&&&&&&&& while (true)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& bytesRead = fileStream.Read(buffer, 0, buffer.Length);&
&&&&&&&&&&&&&&&&&& if (bytesRead == 0)&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&& requestStream.Write(buffer, 0, bytesRead);&
&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& requestStream.Close();&
&&&&&&&&&&&&&& uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();&
&&&&&&&&&& }&
&&&&&&&&&& catch (Exception ex)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& throw new Exception(&上传文件到ftp服务器出错,文件名:& + upLoadFile + &异常信息:& + ex.ToString());&
&&&&&&&&&& }&
&&&&&&&&&& finally&
&&&&&&&&&& {&
&&&&&&&&&&&&&& if (uploadResponse != null)&
&&&&&&&&&&&&&&&&&& uploadResponse.Close();&
&&&&&&&&&&&&&& if (fileStream != null)&
&&&&&&&&&&&&&&&&&& fileStream.Close();&
&&&&&&&&&&&&&& if (requestStream != null)&
&&&&&&&&&&&&&&&&&& requestStream.Close();&
&&&&&&&&&& }&
&&&&&& /// &summary&&&
&&&&&& /// 从ftp下载文件到本地服务器&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&downloadUrl&&要下载的ftp文件路径,如ftp://192.168.1.104/capture-2.avi&/param&&&
&&&&&& /// &param name=&saveFileUrl&&本地保存文件的路径,如(@&d:\capture-22.avi&&/param&&&
&&&&&& public void DownLoadFile(string downloadUrl, string saveFileUrl)&
&&&&&&&&&& Stream responseStream =&
&&&&&&&&&& FileStream fileStream =&
&&&&&&&&&& StreamReader reader =&
&&&&&&&&&& try&
&&&&&&&&&& {&
&&&&&&&&&&&&&& // string downloadUrl = &ftp://192.168.1.104/capture-2.avi&;&&
&&&&&&&&&&&&&& FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl);&
&&&&&&&&&&&&&& downloadRequest.Method = WebRequestMethods.Ftp.DownloadF&
&&&&&&&&&&&&&& //string ftpUser = &yoyo&;&&
&&&&&&&&&&&&&& //string ftpPassWord = &123456&;&&
&&&&&&&&&&&&&& downloadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);&
&&&&&&&&&&&&&& FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse();&
&&&&&&&&&&&&&& responseStream = downloadResponse.GetResponseStream();&
&&&&&&&&&&&&&& fileStream = File.Create(saveFileUrl);&
&&&&&&&&&&&&&& byte[] buffer = new byte[1024];&
&&&&&&&&&&&&&& int bytesR&
&&&&&&&&&&&&&& while (true)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& bytesRead = responseStream.Read(buffer, 0, buffer.Length);&
&&&&&&&&&&&&&&&&&& if (bytesRead == 0)&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&& fileStream.Write(buffer, 0, bytesRead);&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&&&&&& catch (Exception ex)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& throw new Exception(&从ftp服务器下载文件出错,文件名:& + downloadUrl + &异常信息:& + ex.ToString());&
&&&&&&&&&& }&
&&&&&&&&&& finally&
&&&&&&&&&& {&
&&&&&&&&&&&&&& if (reader != null)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& reader.Close();&
&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& if (responseStream != null)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& responseStream.Close();&
&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& if (fileStream != null)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& fileStream.Close();&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&& /// &summary&&&
&&&&&& /// 从FTP下载文件到本地服务器,支持断点下载&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&ftpUri&&ftp文件路径,如&ftp://localhost/test.txt&&/param&&&
&&&&&& /// &param name=&saveFile&&保存文件的路径,如C:\\test.txt&/param&&&
&&&&&& public void BreakPointDownLoadFile(string ftpUri, string saveFile)&
&&&&&&&&&& System.IO.FileStream fs =&
&&&&&&&&&& System.Net.FtpWebResponse ftpRes =&
&&&&&&&&&& System.IO.Stream resStrm =&
&&&&&&&&&& try&
&&&&&&&&&& {&
&&&&&&&&&&&&&& //下载文件的URI&&
&&&&&&&&&&&&&& Uri u = new Uri(ftpUri);&
&&&&&&&&&&&&&& //设定下载文件的保存路径&&
&&&&&&&&&&&&&& string downFile = saveF&
&&&&&&&&&&&&&& //FtpWebRequest的作成&&
&&&&&&&&&&&&&& System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)&
&&&&&&&&&&&&&&&&&& System.Net.WebRequest.Create(u);&
&&&&&&&&&&&&&& //设定用户名和密码&&
&&&&&&&&&&&&&& ftpReq.Credentials = new System.Net.NetworkCredential(ftpUser, ftpPassWord);&
&&&&&&&&&&&&&& //MethodにWebRequestMethods.Ftp.DownloadFile(&RETR&)设定&&
&&&&&&&&&&&&&& ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadF&
&&&&&&&&&&&&&& //要求终了后关闭连接&&
&&&&&&&&&&&&&& ftpReq.KeepAlive =&
&&&&&&&&&&&&&& //使用ASCII方式传送&&
&&&&&&&&&&&&&& ftpReq.UseBinary =&
&&&&&&&&&&&&&& //设定PASSIVE方式无效&&
&&&&&&&&&&&&&& ftpReq.UsePassive =&
&&&&&&&&&&&&&& //判断是否继续下载&&
&&&&&&&&&&&&&& //继续写入下载文件的FileStream&&
&&&&&&&&&&&&&& if (System.IO.File.Exists(downFile))&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& //继续下载&&
&&&&&&&&&&&&&&&&&& ftpReq.ContentOffset = (new System.IO.FileInfo(downFile)).L&
&&&&&&&&&&&&&&&&&& fs = new System.IO.FileStream(&
&&&&&&&&&&&&&&&&&&&&& downFile, System.IO.FileMode.Append, System.IO.FileAccess.Write);&
&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& else&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& //一般下载&&
&&&&&&&&&&&&&&&&&& fs = new System.IO.FileStream(&
&&&&&&&&&&&&&&&&&&&&&& downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);&
&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& //取得FtpWebResponse&&
&&&&&&&&&&&&&& ftpRes = (System.Net.FtpWebResponse)ftpReq.GetResponse();&
&&&&&&&&&&&&&& //为了下载文件取得Stream&&
&&&&&&&&&&&&&& resStrm = ftpRes.GetResponseStream();&
&&&&&&&&&&&&&& //写入下载的数据&&
&&&&&&&&&&&&&& byte[] buffer = new byte[1024];&
&&&&&&&&&&&&&& while (true)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& int readSize = resStrm.Read(buffer, 0, buffer.Length);&
&&&&&&&&&&&&&&&&&& if (readSize == 0)&
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&& fs.Write(buffer, 0, readSize);&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&&&&&& catch (Exception ex)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& throw new Exception(&从ftp服务器下载文件出错,文件名:& + ftpUri + &异常信息:& + ex.ToString());&
&&&&&&&&&& }&
&&&&&&&&&& finally&
&&&&&&&&&& {&
&&&&&&&&&&&&&& fs.Close();&
&&&&&&&&&&&&&& resStrm.Close();&
&&&&&&&&&&&&&& ftpRes.Close();&
&&&&&&&&&& }&
&&&&&& #region 从FTP上下载整个文件夹,包括文件夹下的文件和文件夹&&
&&&&&& /// &summary&&&
&&&&&& /// 列出FTP服务器上面当前目录的所有文件和目录&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&ftpUri&&FTP目录&/param&&&
&&&&&& /// &returns&&/returns&&&
&&&&&& public List&FileStruct& ListFilesAndDirectories(string ftpUri)&
&&&&&&&&&& WebResponse webresp =&
&&&&&&&&&& StreamReader ftpFileListReader =&
&&&&&&&&&& FtpWebRequest ftpRequest =&
&&&&&&&&&& try&
&&&&&&&&&& {&
&&&&&&&&&&&&&& ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpUri));&
&&&&&&&&&&&&&& ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryD&
&&&&&&&&&&&&&& ftpRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);&
&&&&&&&&&&&&&& webresp = ftpRequest.GetResponse();&
&&&&&&&&&&&&&& ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default);&
&&&&&&&&&& }&
&&&&&&&&&& catch (Exception ex)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& throw new Exception(&获取文件列表出错,错误信息如下:& + ex.ToString());&
&&&&&&&&&& }&
&&&&&&&&&& string Datastring = ftpFileListReader.ReadToEnd();&
&&&&&&&&&& return GetList(Datastring);&
&&&&&& /// &summary&&&
&&&&&& /// 列出FTP目录下的所有文件&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&ftpUri&&FTP目录&/param&&&
&&&&&& /// &returns&&/returns&&&
&&&&&& public List&FileStruct& ListFiles(string ftpUri)&
&&&&&&&&&& List&FileStruct& listAll = ListFilesAndDirectories(ftpUri);&
&&&&&&&&&& List&FileStruct& listFile = new List&FileStruct&();&
&&&&&&&&&& foreach (FileStruct file in listAll)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& if (!file.IsDirectory)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& listFile.Add(file);&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&&&&&& return listF&
&&&&&& /// &summary&&&
&&&&&& /// 列出FTP目录下的所有目录&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&ftpUri&&FRTP目录&/param&&&
&&&&&& /// &returns&目录列表&/returns&&&
&&&&&& public List&FileStruct& ListDirectories(string ftpUri)&
&&&&&&&&&& List&FileStruct& listAll = ListFilesAndDirectories(ftpUri);&
&&&&&&&&&& List&FileStruct& listDirectory = new List&FileStruct&();&
&&&&&&&&&& foreach (FileStruct file in listAll)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& if (file.IsDirectory)&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& listDirectory.Add(file);&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&&&&&& return listD&
&&&&&& /// &summary&&&
&&&&&& /// 获得文件和目录列表&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&datastring&&FTP返回的列表字符信息&/param&&&
&&&&&& 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);&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&& case FileListStyle.WindowsStyle:&
&&&&&&&&&&&&&&&&&&&&&&&&&& f = ParseFileStructFromWindowsStyleRecord(s);&
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&&&&&& if (!(f.Name == &.& || f.Name == &..&))&
&&&&&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&&&&&& myListArray.Add(f);&
&&&&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&&&&&& return myListA&
&&&&&& /// &summary&&&
&&&&&& /// 从Unix格式中返回文件信息&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&Record&&文件信息&/param&&&
&&&&&& private FileStruct ParseFileStructFromUnixStyleRecord(string Record)&
&&&&&&&&&& FileStruct f = new FileStruct();&
&&&&&&&&&& string processstr = Record.Trim();&
&&&&&&&&&& f.Flags = processstr.Substring(0, 10);&
&&&&&&&&&& f.IsDirectory = (f.Flags[0] == 'd');&
&&&&&&&&&& processstr = (processstr.Substring(11)).Trim();&
&&&&&&&&&& _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);&& //跳过一部分&&
&&&&&&&&&& f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);&
&&&&&&&&&& f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);&
&&&&&&&&&& _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);&& //跳过一部分&&
&&&&&&&&&& string yearOrTime = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2];&
&&&&&&&&&& if (yearOrTime.IndexOf(&:&) &= 0)& //time&&
&&&&&&&&&& {&
&&&&&&&&&&&&&& processstr = processstr.Replace(yearOrTime, DateTime.Now.Year.ToString());&
&&&&&&&&&& }&
&&&&&&&&&& f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8));&
&&&&&&&&&& f.Name =&& //最后就是名称&&
&&&&&&&&&&&
&&&&&& /// &summary&&&
&&&&&& /// 从Windows格式中返回文件信息&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&Record&&文件信息&/param&&&
&&&&&& private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)&
&&&&&&&&&& FileStruct f = new FileStruct();&
&&&&&&&&&& string processstr = Record.Trim();&
&&&&&&&&&& string dateStr = processstr.Substring(0, 8);&
&&&&&&&&&& processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();&
&&&&&&&&&& string timeStr = processstr.Substring(0, 7);&
&&&&&&&&&& processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();&
&&&&&&&&&& DateTimeFormatInfo myDTFI = new CultureInfo(&en-US&, false).DateTimeF&
&&&&&&&&&& myDTFI.ShortTimePattern = &t&;&
&&&&&&&&&& f.CreateTime = DateTime.Parse(dateStr + & & + timeStr, myDTFI);&
&&&&&&&&&& if (processstr.Substring(0, 5) == &&DIR&&)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& f.IsDirectory =&
&&&&&&&&&&&&&& processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();&
&&&&&&&&&& }&
&&&&&&&&&& else&
&&&&&&&&&& {&
&&&&&&&&&&&&&& string[] strs = processstr.Split(new char[] { ' ' },2);// StringSplitOptions.RemoveEmptyEntries);&& // true);&&
&&&&&&&&&&&&&& processstr = strs[1];&
&&&&&&&&&&&&&& f.IsDirectory =&
&&&&&&&&&& }&
&&&&&&&&&& f.Name =&
&&&&&&&&&&&
&&&&&& /// &summary&&&
&&&&&& /// 按照一定的规则进行字符串截取&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&s&&截取的字符串&/param&&&
&&&&&& /// &param name=&c&&查找的字符&/param&&&
&&&&&& /// &param name=&startIndex&&查找的位置&/param&&&
&&&&&& private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)&
&&&&&&&&&& int pos1 = s.IndexOf(c, startIndex);&
&&&&&&&&&& string retString = s.Substring(0, pos1);&
&&&&&&&&&& s = (s.Substring(pos1)).Trim();&
&&&&&&&&&& return retS&
&&&&&& /// &summary&&&
&&&&&& /// 判断文件列表的方式Window方式还是Unix方式&&
&&&&&& /// &/summary&&&
&&&&&& /// &param name=&recordList&&文件信息列表&/param&&&
&&&&&& private FileListStyle GuessFileListStyle(string[] recordList)&
&&&&&&&&&& foreach (string s in recordList)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& if (s.Length & 10&
&&&&&&&&&&&&&&& && Regex.IsMatch(s.Substring(0, 10), &(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)&))&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& return FileListStyle.UnixS&
&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& else if (s.Length & 8&
&&&&&&&&&&&&&&& && Regex.IsMatch(s.Substring(0, 8), &[0-9][0-9]-[0-9][0-9]-[0-9][0-9]&))&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& return FileListStyle.WindowsS&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&&&&&& return FileListStyle.U&
&&&&&& /// &summary&&&&&
&&&&&& /// 从FTP下载整个文件夹&&&&
&&&&&& /// &/summary&&&&&
&&&&&& /// &param name=&ftpDir&&FTP文件夹路径&/param&&&&&
&&&&&& /// &param name=&saveDir&&保存的本地文件夹路径&/param&&&&&
&&&&&& public void DownFtpDir(string ftpDir, string saveDir)&
&&&&&&&&&& List&FileStruct& files = ListFilesAndDirectories(ftpDir);&
&&&&&&&&&& if (!Directory.Exists(saveDir))&
&&&&&&&&&& {&
&&&&&&&&&&&&&& Directory.CreateDirectory(saveDir);&
&&&&&&&&&& }&
&&&&&&&&&& foreach (FileStruct f in files)&
&&&&&&&&&& {&
&&&&&&&&&&&&&& if (f.IsDirectory) //文件夹,递归查询&&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& DownFtpDir(ftpDir + &/& + f.Name, saveDir + &\\& + f.Name);&
&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&& else //文件,直接下载&&
&&&&&&&&&&&&&& {&
&&&&&&&&&&&&&&&&&& DownLoadFile(ftpDir + &/& + f.Name, saveDir + &\\& + f.Name);&
&&&&&&&&&&&&&& }&
&&&&&&&&&& }&
&&&&&& #endregion&&
&& #region 文件信息结构&&
&& public struct FileStruct&
&&&&&& public string F&
&&&&&& public string O&
&&&&&& public string G&
&&&&&& public bool IsD&
&&&&&& public DateTime CreateT&
&&&&&& public string N&
&& public enum FileListStyle&
&&&&&& UnixStyle,&
&&&&&& WindowsStyle,&
&&&&&& Unknown&
&& #endregion&
&/// &summary&
&&& /// ftp文件上传、下载操作类
&&& /// &/summary&
&&& public class FTPHelper
&&&&&&& /// &summary&
&&&&&&& /// ftp用户名,匿名为&&
&&&&&&& /// &/summary&
&&&&&&& private string ftpU
&&&&&&& /// &summary&
&&&&&&& /// ftp用户密码,匿名为&&
&&&&&&& /// &/summary&
&&&&&&& private string ftpPassW
&&&&&&& /// &summary&
&&&&&&& ///通过用户名,密码连接到FTP服务器
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&ftpUser&&ftp用户名,匿名为&&&/param&
&&&&&&& /// &param name=&ftpPassWord&&ftp登陆密码,匿名为&&&/param&
&&&&&&& public FTPHelper(string ftpUser, string ftpPassWord)
&&&&&&&&&&& this.ftpUser = ftpU
&&&&&&&&&&& this.ftpPassWord = ftpPassW
&&&&&&& /// &summary&
&&&&&&& /// 匿名访问
&&&&&&& /// &/summary&
&&&&&&& public FTPHelper()
&&&&&&&&&&& this.ftpUser = &&;
&&&&&&&&&&& this.ftpPassWord = &&;
&&&&&&& /// &summary&
&&&&&&& /// 上传文件到Ftp服务器
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&uri&&把上传的文件保存为ftp服务器文件的uri,如&ftp://192.168.1.104/capture-212.avi&&/param&
&&&&&&& /// &param name=&upLoadFile&&要上传的本地的文件路径,如D:\capture-2.avi&/param&
&&&&&&& public void UpLoadFile(string UpLoadUri, string upLoadFile)
&&&&&&&&&&& Stream requestStream =
&&&&&&&&&&& FileStream fileStream =
&&&&&&&&&&& FtpWebResponse uploadResponse =
&&&&&&&&&&& try
&&&&&&&&&&& {
&&&&&&&&&&&&&&& Uri uri = new Uri(UpLoadUri);
&&&&&&&&&&&&&&& FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);
&&&&&&&&&&&&&&& uploadRequest.Method = WebRequestMethods.Ftp.UploadF
&&&&&&&&&&&&&&& uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);
&&&&&&&&&&&&&&& requestStream = uploadRequest.GetRequestStream();
&&&&&&&&&&&&&&& fileStream = File.Open(upLoadFile, FileMode.Open);
&&&&&&&&&&&&&&& byte[] buffer = new byte[1024];
&&&&&&&&&&&&&&& int bytesR
&&&&&&&&&&&&&&& while (true)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& bytesRead = fileStream.Read(buffer, 0, buffer.Length);
&&&&&&&&&&&&&&&&&&& if (bytesRead == 0)
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& requestStream.Write(buffer, 0, bytesRead);
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& requestStream.Close();
&&&&&&&&&&&&&&& uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
&&&&&&&&&&& }
&&&&&&&&&&& catch (Exception ex)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& throw new Exception(&上传文件到ftp服务器出错,文件名:& + upLoadFile + &异常信息:& + ex.ToString());
&&&&&&&&&&& }
&&&&&&&&&&& finally
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if (uploadResponse != null)
&&&&&&&&&&&&&&&&&&& uploadResponse.Close();
&&&&&&&&&&&&&&& if (fileStream != null)
&&&&&&&&&&&&&&&&&&& fileStream.Close();
&&&&&&&&&&&&&&& if (requestStream != null)
&&&&&&&&&&&&&&&&&&& requestStream.Close();
&&&&&&&&&&& }
&&&&&&& /// &summary&
&&&&&&& /// 从ftp下载文件到本地服务器
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&downloadUrl&&要下载的ftp文件路径,如ftp://192.168.1.104/capture-2.avi&/param&
&&&&&&& /// &param name=&saveFileUrl&&本地保存文件的路径,如(@&d:\capture-22.avi&&/param&
&&&&&&& public void DownLoadFile(string downloadUrl, string saveFileUrl)
&&&&&&&&&&& Stream responseStream =
&&&&&&&&&&& FileStream fileStream =
&&&&&&&&&&& StreamReader reader =
&&&&&&&&&&& try
&&&&&&&&&&& {
&&&&&&&&&&&&&&& // string downloadUrl = &ftp://192.168.1.104/capture-2.avi&;
&&&&&&&&&&&&&&& FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl);
&&&&&&&&&&&&&&& downloadRequest.Method = WebRequestMethods.Ftp.DownloadF
&&&&&&&&&&&&&&& //string ftpUser = &yoyo&;
&&&&&&&&&&&&&&& //string ftpPassWord = &123456&;
&&&&&&&&&&&&&&& downloadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);
&&&&&&&&&&&&&&& FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse();
&&&&&&&&&&&&&&& responseStream = downloadResponse.GetResponseStream();
&&&&&&&&&&&&&&& fileStream = File.Create(saveFileUrl);
&&&&&&&&&&&&&&& byte[] buffer = new byte[1024];
&&&&&&&&&&&&&&& int bytesR
&&&&&&&&&&&&&&& while (true)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& bytesRead = responseStream.Read(buffer, 0, buffer.Length);
&&&&&&&&&&&&&&&&&&& if (bytesRead == 0)
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& fileStream.Write(buffer, 0, bytesRead);
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&&&&&& catch (Exception ex)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& throw new Exception(&从ftp服务器下载文件出错,文件名:& + downloadUrl + &异常信息:& + ex.ToString());
&&&&&&&&&&& }
&&&&&&&&&&& finally
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if (reader != null)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& reader.Close();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& if (responseStream != null)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& responseStream.Close();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& if (fileStream != null)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& fileStream.Close();
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&& /// &summary&
&&&&&&& /// 从FTP下载文件到本地服务器,支持断点下载
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&ftpUri&&ftp文件路径,如&ftp://localhost/test.txt&&/param&
&&&&&&& /// &param name=&saveFile&&保存文件的路径,如C:\\test.txt&/param&
&&&&&&& public void BreakPointDownLoadFile(string ftpUri, string saveFile)
&&&&&&&&&&& System.IO.FileStream fs =
&&&&&&&&&&& System.Net.FtpWebResponse ftpRes =
&&&&&&&&&&& System.IO.Stream resStrm =
&&&&&&&&&&& try
&&&&&&&&&&& {
&&&&&&&&&&&&&&& //下载文件的URI
&&&&&&&&&&&&&&& Uri u = new Uri(ftpUri);
&&&&&&&&&&&&&&& //设定下载文件的保存路径
&&&&&&&&&&&&&&& string downFile = saveF
&&&&&&&&&&&&&&& //FtpWebRequest的作成
&&&&&&&&&&&&&&& System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)
&&&&&&&&&&&&&&&&&&& System.Net.WebRequest.Create(u);
&&&&&&&&&&&&&&& //设定用户名和密码
&&&&&&&&&&&&&&& ftpReq.Credentials = new System.Net.NetworkCredential(ftpUser, ftpPassWord);
&&&&&&&&&&&&&&& //MethodにWebRequestMethods.Ftp.DownloadFile(&RETR&)设定
&&&&&&&&&&&&&&& ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadF
&&&&&&&&&&&&&&& //要求终了后关闭连接
&&&&&&&&&&&&&&& ftpReq.KeepAlive =
&&&&&&&&&&&&&&& //使用ASCII方式传送
&&&&&&&&&&&&&&& ftpReq.UseBinary =
&&&&&&&&&&&&&&& //设定PASSIVE方式无效
&&&&&&&&&&&&&&& ftpReq.UsePassive =
&&&&&&&&&&&&&&& //判断是否继续下载
&&&&&&&&&&&&&&& //继续写入下载文件的FileStream
&&&&&&&&&&&&&&& if (System.IO.File.Exists(downFile))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& //继续下载
&&&&&&&&&&&&&&&&&&& ftpReq.ContentOffset = (new System.IO.FileInfo(downFile)).L
&&&&&&&&&&&&&&&&&&& fs = new System.IO.FileStream(
&&&&&&&&&&&&&&&&&&&&&& downFile, System.IO.FileMode.Append, System.IO.FileAccess.Write);
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& else
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& //一般下载
&&&&&&&&&&&&&&&&&&& fs = new System.IO.FileStream(
&&&&&&&&&&&&&&&&&&&&&&& downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& //取得FtpWebResponse
&&&&&&&&&&&&&&& ftpRes = (System.Net.FtpWebResponse)ftpReq.GetResponse();
&&&&&&&&&&&&&&& //为了下载文件取得Stream
&&&&&&&&&&&&&&& resStrm = ftpRes.GetResponseStream();
&&&&&&&&&&&&&&& //写入下载的数据
&&&&&&&&&&&&&&& byte[] buffer = new byte[1024];
&&&&&&&&&&&&&&& while (true)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& int readSize = resStrm.Read(buffer, 0, buffer.Length);
&&&&&&&&&&&&&&&&&&& if (readSize == 0)
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& fs.Write(buffer, 0, readSize);
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&&&&&& catch (Exception ex)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& throw new Exception(&从ftp服务器下载文件出错,文件名:& + ftpUri + &异常信息:& + ex.ToString());
&&&&&&&&&&& }
&&&&&&&&&&& finally
&&&&&&&&&&& {
&&&&&&&&&&&&&&& fs.Close();
&&&&&&&&&&&&&&& resStrm.Close();
&&&&&&&&&&&&&&& ftpRes.Close();
&&&&&&&&&&& }
&&&&&&& #region 从FTP上下载整个文件夹,包括文件夹下的文件和文件夹
&&&&&&& /// &summary&
&&&&&&& /// 列出FTP服务器上面当前目录的所有文件和目录
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&ftpUri&&FTP目录&/param&
&&&&&&& /// &returns&&/returns&
&&&&&&& public List&FileStruct& ListFilesAndDirectories(string ftpUri)
&&&&&&&&&&& WebResponse webresp =
&&&&&&&&&&& StreamReader ftpFileListReader =
&&&&&&&&&&& FtpWebRequest ftpRequest =
&&&&&&&&&&& try
&&&&&&&&&&& {
&&&&&&&&&&&&&&& ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpUri));
&&&&&&&&&&&&&&& ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryD
&&&&&&&&&&&&&&& ftpRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);
&&&&&&&&&&&&&&& webresp = ftpRequest.GetResponse();
&&&&&&&&&&&&&&& ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default);
&&&&&&&&&&& }
&&&&&&&&&&& catch (Exception ex)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& throw new Exception(&获取文件列表出错,错误信息如下:& + ex.ToString());
&&&&&&&&&&& }
&&&&&&&&&&& string Datastring = ftpFileListReader.ReadToEnd();
&&&&&&&&&&& return GetList(Datastring);
&&&&&&& /// &summary&
&&&&&&& /// 列出FTP目录下的所有文件
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&ftpUri&&FTP目录&/param&
&&&&&&& /// &returns&&/returns&
&&&&&&& public List&FileStruct& ListFiles(string ftpUri)
&&&&&&&&&&& List&FileStruct& listAll = ListFilesAndDirectories(ftpUri);
&&&&&&&&&&& List&FileStruct& listFile = new List&FileStruct&();
&&&&&&&&&&& foreach (FileStruct file in listAll)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if (!file.IsDirectory)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& listFile.Add(file);
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&&&&&& return listF
&&&&&&& /// &summary&
&&&&&&& /// 列出FTP目录下的所有目录
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&ftpUri&&FRTP目录&/param&
&&&&&&& /// &returns&目录列表&/returns&
&&&&&&& public List&FileStruct& ListDirectories(string ftpUri)
&&&&&&&&&&& List&FileStruct& listAll = ListFilesAndDirectories(ftpUri);
&&&&&&&&&&& List&FileStruct& listDirectory = new List&FileStruct&();
&&&&&&&&&&& foreach (FileStruct file in listAll)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if (file.IsDirectory)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& listDirectory.Add(file);
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&&&&&& return listD
&&&&&&& /// &summary&
&&&&&&& /// 获得文件和目录列表
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&datastring&&FTP返回的列表字符信息&/param&
&&&&&&& 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);
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&& case FileListStyle.WindowsStyle:
&&&&&&&&&&&&&&&&&&&&&&&&&&& f = ParseFileStructFromWindowsStyleRecord(s);
&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&& if (!(f.Name == &.& || f.Name == &..&))
&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&& myListArray.Add(f);
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&&&&&& return myListA
&&&&&&& /// &summary&
&&&&&&& /// 从Unix格式中返回文件信息
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&Record&&文件信息&/param&
&&&&&&& private FileStruct ParseFileStructFromUnixStyleRecord(string Record)
&&&&&&&&&&& FileStruct f = new FileStruct();
&&&&&&&&&&& string processstr = Record.Trim();
&&&&&&&&&&& f.Flags = processstr.Substring(0, 10);
&&&&&&&&&&& f.IsDirectory = (f.Flags[0] == 'd');
&&&&&&&&&&& processstr = (processstr.Substring(11)).Trim();
&&&&&&&&&&& _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);&& //跳过一部分
&&&&&&&&&&& f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
&&&&&&&&&&& f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
&&&&&&&&&&& _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);&& //跳过一部分
&&&&&&&&&&& string yearOrTime = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2];
&&&&&&&&&&& if (yearOrTime.IndexOf(&:&) &= 0)& //time
&&&&&&&&&&& {
&&&&&&&&&&&&&&& processstr = processstr.Replace(yearOrTime, DateTime.Now.Year.ToString());
&&&&&&&&&&& }
&&&&&&&&&&& f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8));
&&&&&&&&&&& f.Name =&& //最后就是名称
&&&&&&&&&&&
&&&&&&& /// &summary&
&&&&&&& /// 从Windows格式中返回文件信息
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&Record&&文件信息&/param&
&&&&&&& private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
&&&&&&&&&&& FileStruct f = new FileStruct();
&&&&&&&&&&& string processstr = Record.Trim();
&&&&&&&&&&& string dateStr = processstr.Substring(0, 8);
&&&&&&&&&&& processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
&&&&&&&&&&& string timeStr = processstr.Substring(0, 7);
&&&&&&&&&&& processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
&&&&&&&&&&& DateTimeFormatInfo myDTFI = new CultureInfo(&en-US&, false).DateTimeF
&&&&&&&&&&& myDTFI.ShortTimePattern = &t&;
&&&&&&&&&&& f.CreateTime = DateTime.Parse(dateStr + & & + timeStr, myDTFI);
&&&&&&&&&&& if (processstr.Substring(0, 5) == &&DIR&&)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& f.IsDirectory =
&&&&&&&&&&&&&&& processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
&&&&&&&&&&& }
&&&&&&&&&&& else
&&&&&&&&&&& {
&&&&&&&&&&&&&&& string[] strs = processstr.Split(new char[] { ' ' },2);// StringSplitOptions.RemoveEmptyEntries);&& // true);
&&&&&&&&&&&&&&& processstr = strs[1];
&&&&&&&&&&&&&&& f.IsDirectory =
&&&&&&&&&&& }
&&&&&&&&&&& f.Name =
&&&&&&&&&&&
&&&&&&& /// &summary&
&&&&&&& /// 按照一定的规则进行字符串截取
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&s&&截取的字符串&/param&
&&&&&&& /// &param name=&c&&查找的字符&/param&
&&&&&&& /// &param name=&startIndex&&查找的位置&/param&
&&&&&&& private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)
&&&&&&&&&&& int pos1 = s.IndexOf(c, startIndex);
&&&&&&&&&&& string retString = s.Substring(0, pos1);
&&&&&&&&&&& s = (s.Substring(pos1)).Trim();
&&&&&&&&&&& return retS
&&&&&&& /// &summary&
&&&&&&& /// 判断文件列表的方式Window方式还是Unix方式
&&&&&&& /// &/summary&
&&&&&&& /// &param name=&recordList&&文件信息列表&/param&
&&&&&&& private FileListStyle GuessFileListStyle(string[] recordList)
&&&&&&&&&&& foreach (string s in recordList)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if (s.Length & 10
&&&&&&&&&&&&&&&& && Regex.IsMatch(s.Substring(0, 10), &(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)&))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& return FileListStyle.UnixS
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& else if (s.Length & 8
&&&&&&&&&&&&&&&& && Regex.IsMatch(s.Substring(0, 8), &[0-9][0-9]-[0-9][0-9]-[0-9][0-9]&))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& return FileListStyle.WindowsS
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&&&&&& return FileListStyle.U
&&&&&&& /// &summary&&
&&&&&&& /// 从FTP下载整个文件夹&
&&&&&&& /// &/summary&&
&&&&&&& /// &param name=&ftpDir&&FTP文件夹路径&/param&&
&&&&&&& /// &param name=&saveDir&&保存的本地文件夹路径&/param&&
&&&&&&& public void DownFtpDir(string ftpDir, string saveDir)
&&&&&&&&&&& List&FileStruct& files = ListFilesAndDirectories(ftpDir);
&&&&&&&&&&& if (!Directory.Exists(saveDir))
&&&&&&&&&&& {
&&&&&&&&&&&&&&& Directory.CreateDirectory(saveDir);
&&&&&&&&&&& }
&&&&&&&&&&& foreach (FileStruct f in files)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if (f.IsDirectory) //文件夹,递归查询
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& DownFtpDir(ftpDir + &/& + f.Name, saveDir + &\\& + f.Name);
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& else //文件,直接下载
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& DownLoadFile(ftpDir + &/& + f.Name, saveDir + &\\& + f.Name);
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&& #endregion
&&& #region 文件信息结构
&&& public struct FileStruct
&&&&&&& public string F
&&&&&&& public string O
&&&&&&& public string G
&&&&&&& public bool IsD
&&&&&&& public DateTime CreateT
&&&&&&& public string N
&&& public enum FileListStyle
&&&&&&& UnixStyle,
&&&&&&& WindowsStyle,
&&&&&&& Unknown
&&& #endregion
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 excel生成目录索引 的文章

 

随机推荐