1,FTP服务创建于配置http://jingyan.baidu.com/article/0a52e3f4230067bf63ed7268.html,

2,FTP操作类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace FTP
  11. {
  12. //使用时需要建立配置文件在配置文件里面写链接信息,
  13. public class FtpHelper
  14. {
  15. //基本设置
  16. public static string path = @"ftp://" + FtpHelper.GetAppConfig("path") + "/"; //目标路径
  17. public static string ftpip = FtpHelper.GetAppConfig("path"); //ftp IP地址
  18. static private string username = FtpHelper.GetAppConfig("username"); //ftp用户名
  19. static private string password = FtpHelper.GetAppConfig("password"); //ftp密码
  20. public static string GetAppConfig(string strKey)
  21. {
  22. foreach (string key in ConfigurationManager.AppSettings)
  23. {
  24. if (key == strKey)
  25. {
  26. string str = ConfigurationManager.AppSettings[strKey];
  27. return ConfigurationManager.AppSettings[strKey];
  28. }
  29. }
  30. return null;
  31. }
  32. public static string[] GetFileList()//已验证
  33. {
  34. //dir = dir + "/";//加斜线与不加斜线的区别是路径和文件名
  35. string[] downloadFiles;
  36. StringBuilder result = new StringBuilder();
  37. FtpWebRequest request;
  38. try
  39. {
  40. request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
  41. request.UseBinary = true;
  42. request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
  43. request.Method = WebRequestMethods.Ftp.ListDirectory;
  44. request.UseBinary = true;
  45.  
  46. WebResponse response = request.GetResponse();
  47. StreamReader reader = new StreamReader(response.GetResponseStream());
  48.  
  49. string line = reader.ReadLine();
  50. while (line != null)
  51. {
  52. result.Append(line);
  53. result.Append("\n");
  54. Console.WriteLine(line);
  55. line = reader.ReadLine();
  56. }
  57. // to remove the trailing '\n'
  58. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  59. reader.Close();
  60. response.Close();
  61. return result.ToString().Split('\n');
  62. }
  63. catch (Exception ex)
  64. {
  65. Console.WriteLine("获取ftp上面的文件和文件夹:" + ex.Message);
  66. downloadFiles = null;
  67. return downloadFiles;
  68. }
  69. }
  70.  
  71. /// <summary>
  72. /// 获取文件大小
  73. /// </summary>
  74. /// <param name="file">ip服务器下的相对路径</param>
  75. /// <returns>文件大小</returns>
  76. public static int GetFileSize(string file)//已验证
  77. {
  78. StringBuilder result = new StringBuilder();
  79. FtpWebRequest request;
  80. try
  81. {
  82. request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + file));
  83. request.UseBinary = true;
  84. request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
  85. request.Method = WebRequestMethods.Ftp.GetFileSize;
  86.  
  87. int dataLength = (int)request.GetResponse().ContentLength;
  88.  
  89. return dataLength;
  90. }
  91. catch (Exception ex)
  92. {
  93. Console.WriteLine("获取文件大小出错:" + ex.Message);
  94. return -1;
  95. }
  96. }
  97.  
  98. /// <summary>
  99. /// 文件上传
  100. /// </summary>
  101. /// <param name="filePath">原路径(绝对路径)包括文件名</param>
  102. /// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param>
  103. public static void FileUpLoad(string filePath, string objPath = "")
  104. {
  105. try
  106. {
  107. string url = path;
  108. if (objPath != "")
  109. url += objPath + "/";
  110. try
  111. {
  112.  
  113. FtpWebRequest reqFTP = null;
  114. //待上传的文件 (全路径)
  115. try
  116. {
  117. FileInfo fileInfo = new FileInfo(filePath);
  118. using (FileStream fs = fileInfo.OpenRead())
  119. {
  120. long length = fs.Length;
  121. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + fileInfo.Name[0]));
  122.  
  123. //设置连接到FTP的帐号密码
  124. reqFTP.Credentials = new NetworkCredential(username, password);
  125. //设置请求完成后是否保持连接
  126. reqFTP.KeepAlive = false;
  127. //指定执行命令
  128. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  129. //指定数据传输类型
  130. reqFTP.UseBinary = true;
  131.  
  132. using (Stream stream = reqFTP.GetRequestStream())
  133. {
  134. //设置缓冲大小
  135. int BufferLength = 5120;
  136. byte[] b = new byte[BufferLength];
  137. int i;
  138. while ((i = fs.Read(b, 0, BufferLength)) > 0)
  139. {
  140. stream.Write(b, 0, i);
  141. }
  142. Console.WriteLine("上传文件成功");
  143. }
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. Console.WriteLine("上传文件失败错误为" + ex.Message);
  149. }
  150. finally
  151. {
  152.  
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. Console.WriteLine("上传文件失败错误为" + ex.Message);
  158. }
  159. finally
  160. {
  161.  
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. Console.WriteLine("上传文件失败错误为" + ex.Message);
  167. }
  168. }
  169.  
  170. /// <summary>
  171. /// 删除文件
  172. /// </summary>
  173. /// <param name="fileName">服务器下的相对路径 包括文件名</param>
  174. public static void DeleteFileName(string fileName)//已验证
  175. {
  176. try
  177. {
  178. FileInfo fileInf = new FileInfo(ftpip + "" + fileName);
  179. string uri = path + fileName;
  180. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  181. // 指定数据传输类型
  182. reqFTP.UseBinary = true;
  183. // ftp用户名和密码
  184. reqFTP.Credentials = new NetworkCredential(username, password);
  185. // 默认为true,连接不会被关闭
  186. // 在一个命令之后被执行
  187. reqFTP.KeepAlive = false;
  188. // 指定执行什么命令
  189. reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
  190. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  191. response.Close();
  192. }
  193. catch (Exception ex)
  194. {
  195. Console.WriteLine("删除文件出错:" + ex.Message);
  196. }
  197. }
  198. /// <summary>
  199. /// 删除文件
  200. /// </summary>
  201. /// <param name="fileName"></param>
  202. public static void Delete(string fileName)//已验证
  203. {
  204. try
  205. {
  206. string uri = path + fileName;
  207. FtpWebRequest reqFTP;
  208. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  209.  
  210. reqFTP.Credentials = new NetworkCredential(username, password);
  211. reqFTP.KeepAlive = false;
  212. reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
  213. string result = String.Empty;
  214. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  215. long size = response.ContentLength;
  216. Stream datastream = response.GetResponseStream();
  217. StreamReader sr = new StreamReader(datastream);
  218. result = sr.ReadToEnd();
  219. sr.Close();
  220. datastream.Close();
  221. response.Close();
  222. }
  223. catch (Exception ex)
  224. {
  225. //Insert_Standard_ErrorLog.Insert("FtpWeb", "Delete Error --> " + ex.Message + "  文件名:" + fileName);
  226. }
  227. }
  228. /// <summary>
  229. /// 删除文件夹
  230. /// </summary>
  231. /// <param name="folderName"></param>
  232. public static void RemoveDirectory(string folderName)//已验证
  233. {
  234. try
  235. {
  236. string uri = path + folderName;
  237. FtpWebRequest reqFTP;
  238. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  239.  
  240. reqFTP.Credentials = new NetworkCredential(username,password);
  241. reqFTP.KeepAlive = false;
  242. reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
  243. string result = String.Empty;
  244. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  245. long size = response.ContentLength;
  246. Stream datastream = response.GetResponseStream();
  247. StreamReader sr = new StreamReader(datastream);
  248. result = sr.ReadToEnd();
  249. sr.Close();
  250. datastream.Close();
  251. response.Close();
  252. }
  253. catch (Exception ex)
  254. {
  255. //Insert_Standard_ErrorLog.Insert("FtpWeb", "Delete Error --> " + ex.Message + "  文件名:" + folderName);
  256. }
  257. }
  258. /// <summary>
  259. /// 获取当前目录下明细(包含文件和文件夹)
  260. /// </summary>
  261. /// <returns></returns>
  262. public static string[] GetFilesDetailList(string dir)
  263. {
  264. string[] downloadFiles;
  265. try
  266. {
  267. dir = dir + "/";
  268. StringBuilder result = new StringBuilder();
  269. FtpWebRequest ftp;
  270. ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path+dir));
  271. ftp.Credentials = new NetworkCredential(username, password);
  272. ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  273. WebResponse response = ftp.GetResponse();
  274. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
  275.  
  276. //while (reader.Read() > 0)
  277. //{
  278.  
  279. //}
  280. string line = reader.ReadLine();
  281. //line = reader.ReadLine();
  282. //line = reader.ReadLine();
  283.  
  284. while (line != null)
  285. {
  286. result.Append(line);
  287. result.Append("\n");
  288. line = reader.ReadLine();
  289. }
  290. result.Remove(result.ToString().LastIndexOf("\n"), 1);
  291. reader.Close();
  292. response.Close();
  293. return result.ToString().Split('\n');
  294. }
  295. catch (Exception ex)
  296. {
  297. downloadFiles = null;
  298. //Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFilesDetailList Error --> " + ex.Message);
  299. return downloadFiles;
  300. }
  301. }
  302. public static string[] GetFilesDetailList()
  303. {
  304. string[] downloadFiles;
  305. try
  306. {
  307.  
  308. StringBuilder result = new StringBuilder();
  309. FtpWebRequest ftp;
  310. ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
  311. ftp.Credentials = new NetworkCredential(username, password);
  312. ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  313. WebResponse response = ftp.GetResponse();
  314. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
  315.  
  316. //while (reader.Read() > 0)
  317. //{
  318.  
  319. //}
  320. string line = reader.ReadLine();
  321. //line = reader.ReadLine();
  322. //line = reader.ReadLine();
  323.  
  324. while (line != null)
  325. {
  326. result.Append(line);
  327. result.Append("\n");
  328. line = reader.ReadLine();
  329. }
  330. result.Remove(result.ToString().LastIndexOf("\n"), 1);
  331. reader.Close();
  332. response.Close();
  333. return result.ToString().Split('\n');
  334. }
  335. catch (Exception ex)
  336. {
  337. downloadFiles = null;
  338. //Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFilesDetailList Error --> " + ex.Message);
  339. return downloadFiles;
  340. }
  341. }
  342. public static void Upload()//已验证
  343. {
  344. OpenFileDialog openFileDialog = new OpenFileDialog();
  345. openFileDialog.Title = "选择文件";
  346. openFileDialog.Filter = "所有文件|*.*|rar文件|*.rar|文本文件|*.txt";
  347. openFileDialog.FileName = string.Empty;
  348. openFileDialog.FilterIndex = 1;
  349. openFileDialog.RestoreDirectory = true;
  350. openFileDialog.DefaultExt = "txt";
  351. DialogResult result = openFileDialog.ShowDialog();
  352. if (result == DialogResult.Cancel)
  353. {
  354. return;
  355. }
  356. string fileName = openFileDialog.FileName;
  357. FileInfo fileInf = new FileInfo(fileName);
  358. string uri = path + fileInf.Name;
  359. FtpWebRequest reqFTP;
  360. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  361. reqFTP.Credentials = new NetworkCredential(username, password);
  362. reqFTP.KeepAlive = false;
  363. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  364. reqFTP.UseBinary = true;
  365. reqFTP.ContentLength = fileInf.Length;
  366. int buffLength = 2048;
  367. byte[] buff = new byte[buffLength];
  368. int contentLen;
  369. FileStream fs = fileInf.OpenRead();
  370. try
  371. {
  372. Stream strm = reqFTP.GetRequestStream();
  373. contentLen = fs.Read(buff, 0, buffLength);
  374. while (contentLen != 0)
  375. {
  376. strm.Write(buff, 0, contentLen);
  377. contentLen = fs.Read(buff, 0, buffLength);
  378. }
  379. strm.Close();
  380. fs.Close();
  381. }
  382. catch (Exception ex)
  383. {
  384. //Insert_Standard_ErrorLog.Insert("FtpWeb", "Upload Error --> " + ex.Message);
  385. }
  386. }
  387. public static void Download(string fileName)//已验证
  388. {
  389.  
  390. SaveFileDialog saveFileDialog = new SaveFileDialog();
  391. saveFileDialog.FileName = fileName;
  392. saveFileDialog.Filter = "所有文件(*.*)|(*.*)";
  393. if (saveFileDialog.ShowDialog() != DialogResult.OK)
  394. {
  395. return;
  396. }
  397.  
  398. string filePath = saveFileDialog.FileName;
  399. FtpWebRequest reqFTP;
  400. try
  401. {
  402. FileStream outputStream = new FileStream(filePath, FileMode.Create);
  403.  
  404. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + fileName));
  405. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  406. reqFTP.UseBinary = true;
  407. reqFTP.Credentials = new NetworkCredential(username, password);
  408. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  409. Stream ftpStream = response.GetResponseStream();
  410. long cl = response.ContentLength;
  411. int bufferSize = 2048;
  412. int readCount;
  413. byte[] buffer = new byte[bufferSize];
  414.  
  415. readCount = ftpStream.Read(buffer, 0, bufferSize);
  416. while (readCount > 0)
  417. {
  418. outputStream.Write(buffer, 0, readCount);
  419. readCount = ftpStream.Read(buffer, 0, bufferSize);
  420. }
  421.  
  422. ftpStream.Close();
  423. outputStream.Close();
  424. response.Close();
  425. }
  426. catch (Exception ex)
  427. {
  428. //Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --> " + ex.Message);
  429. }
  430. }
  431. /// <summary>
  432. /// 新建目录 上一级必须先存在
  433. /// </summary>
  434. /// <param name="dirName">服务器下的相对路径</param>
  435. public static void MakeDir(string dirName)//已验证
  436. {
  437. try
  438. {
  439. string uri = path + dirName;
  440. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  441. // 指定数据传输类型
  442. reqFTP.UseBinary = true;
  443. // ftp用户名和密码
  444. reqFTP.Credentials = new NetworkCredential(username, password);
  445. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  446. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  447. response.Close();
  448. }
  449. catch (Exception ex)
  450. {
  451. Console.WriteLine("创建目录出错:" + ex.Message);
  452. }
  453. }
  454.  
  455. /// <summary>
  456. /// 删除目录 上一级必须先存在
  457. /// </summary>
  458. /// <param name="dirName">服务器下的相对路径</param>
  459. public static void DelDir(string dirName)//已验证
  460. {
  461. try
  462. {
  463. string uri = path + dirName;
  464. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  465. // ftp用户名和密码
  466. reqFTP.Credentials = new NetworkCredential(username, password);
  467. reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
  468. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  469. response.Close();
  470. }
  471. catch (Exception ex)
  472. {
  473. Console.WriteLine("删除目录出错:" + ex.Message);
  474. }
  475. }
  476.  
  477. /// <summary>
  478. /// 从ftp服务器上获得文件夹列表
  479. /// </summary>
  480. /// <param name="RequedstPath">服务器下的相对路径</param>
  481. /// <returns></returns>
  482. public static List<string> GetDirctory(string RequedstPath)//已验证
  483. {
  484. List<string> strs = new List<string>();
  485. try
  486. {
  487. string uri = path + RequedstPath; //目标路径 path为服务器地址
  488. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  489. // ftp用户名和密码
  490. reqFTP.Credentials = new NetworkCredential(username, password);
  491. reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  492. WebResponse response = reqFTP.GetResponse();
  493. StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
  494.  
  495. string line = reader.ReadLine();
  496. while (line != null)
  497. {
  498. if (line.Contains("<DIR>"))
  499. {
  500. string msg = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
  501. strs.Add(msg);
  502. }
  503. line = reader.ReadLine();
  504. }
  505. reader.Close();
  506. response.Close();
  507. return strs;
  508. }
  509. catch (Exception ex)
  510. {
  511. Console.WriteLine("获取目录出错:" + ex.Message);
  512. }
  513. return strs;
  514. }
  515.  
  516. /// <summary>
  517. /// 从ftp服务器上获得文件列表
  518. /// </summary>
  519. /// <param name="RequedstPath">服务器下的相对路径</param>
  520. /// <returns></returns>
  521. public static List<string> GetFile(string RequedstPath)//已验证
  522. {
  523. List<string> strs = new List<string>();
  524. try
  525. {
  526. string uri = path + RequedstPath; //目标路径 path为服务器地址
  527. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  528. // ftp用户名和密码
  529. reqFTP.Credentials = new NetworkCredential(username, password);
  530. reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  531. WebResponse response = reqFTP.GetResponse();
  532. StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
  533.  
  534. string line = reader.ReadLine();
  535. while (line != null)
  536. {
  537. if (!line.Contains("<DIR>"))
  538. {
  539. string msg = line.Substring(39).Trim();
  540. strs.Add(msg);
  541. }
  542. line = reader.ReadLine();
  543. }
  544. reader.Close();
  545. response.Close();
  546. return strs;
  547. }
  548. catch (Exception ex)
  549. {
  550. Console.WriteLine("获取文件出错:" + ex.Message);
  551. }
  552. return strs;
  553. }
  554. /// <summary>
  555. /// 获取当前目录下文件列表(仅文件)
  556. /// </summary>
  557. /// <returns></returns>
  558. public static string[] GetFileList1(string mask)
  559. {
  560. string[] downloadFiles;
  561. StringBuilder result = new StringBuilder();
  562. FtpWebRequest reqFTP;
  563. try
  564. {
  565. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
  566. reqFTP.UseBinary = true;
  567. reqFTP.Credentials = new NetworkCredential(username, password);
  568. reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  569. WebResponse response = reqFTP.GetResponse();
  570. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
  571.  
  572. string line = reader.ReadLine();
  573. while (line != null)
  574. {
  575. if (mask.Trim() != string.Empty && mask.Trim() != "*.*")
  576. {
  577.  
  578. string mask_ = mask.Substring(0, mask.IndexOf("*"));
  579. if (line.Substring(0, mask_.Length) == mask_)
  580. {
  581. result.Append(line);
  582. result.Append("\n");
  583. }
  584. }
  585. else
  586. {
  587. result.Append(line);
  588. result.Append("\n");
  589. }
  590. line = reader.ReadLine();
  591. }
  592. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  593. reader.Close();
  594. response.Close();
  595. return result.ToString().Split('\n');
  596. }
  597. catch (Exception ex)
  598. {
  599. downloadFiles = null;
  600. if (ex.Message.Trim() != "远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)。")
  601. {
  602. //Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileList Error --> " + ex.Message.ToString());
  603. }
  604. return downloadFiles;
  605. }
  606. }
  607.  
  608. /// <summary>
  609. /// 获取当前目录下所有的文件夹列表(仅文件夹)
  610. /// </summary>
  611. /// <returns></returns>
  612. public static string[] GetDirectoryList()
  613. {
  614. string[] drectory = GetFilesDetailList();
  615. string m = string.Empty;
  616. foreach (string str in drectory)
  617. {
  618. int dirPos = str.IndexOf("<DIR>");
  619. if (dirPos > 0)
  620. {
  621. /*判断 Windows 风格*/
  622. m += str.Substring(dirPos + 5).Trim() + "\n";
  623. }
  624. else if (str.Trim().Substring(0, 1).ToUpper() == "D")
  625. {
  626. /*判断 Unix 风格*/
  627. string dir = str.Substring(54).Trim();
  628. if (dir != "." && dir != "..")
  629. {
  630. m += dir + "\n";
  631. }
  632. }
  633. }
  634.  
  635. char[] n = new char[] { '\n' };
  636. return m.Split(n);
  637. }
  638.  
  639. /// <summary>
  640. /// 判断当前目录下指定的子目录是否存在
  641. /// </summary>
  642. /// <param name="RemoteDirectoryName">指定的目录名</param>
  643. public static bool DirectoryExist(string RemoteDirectoryName)
  644. {
  645. string[] dirList = GetDirectoryList();
  646. foreach (string str in dirList)
  647. {
  648. if (str.Trim() == RemoteDirectoryName.Trim())
  649. {
  650. return true;
  651. }
  652. }
  653. return false;
  654. }
  655.  
  656. /// <summary>
  657. /// 判断当前目录下指定的文件是否存在
  658. /// </summary>
  659. /// <param name="RemoteFileName">远程文件名</param>
  660. public static bool FileExist(string RemoteFileName)
  661. {
  662. string[] fileList = GetFileList1("*.*");
  663. foreach (string str in fileList)
  664. {
  665. if (str.Trim() == RemoteFileName.Trim())
  666. {
  667. return true;
  668. }
  669. }
  670. return false;
  671. }
  672. /// <summary>
  673. /// 改名
  674. /// </summary>
  675. public static void ReName(string currentFilename, string newFilename)//已验证
  676. {
  677. FtpWebRequest reqFTP;
  678. try
  679. {
  680. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + currentFilename));
  681. string type = Path.GetExtension(currentFilename);//获取文件扩展名
  682. reqFTP.Method = WebRequestMethods.Ftp.Rename;
  683. reqFTP.RenameTo = newFilename+type;
  684. reqFTP.UseBinary = true;
  685. reqFTP.Credentials = new NetworkCredential(username, password);
  686. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  687. Stream ftpStream = response.GetResponseStream();
  688.  
  689. ftpStream.Close();
  690. response.Close();
  691. }
  692. catch (Exception ex)
  693. {
  694. //Insert_Standard_ErrorLog.Insert("FtpWeb", "ReName Error --> " + ex.Message);
  695. }
  696. }
  697.  
  698. /// <summary>
  699. /// 移动文件
  700. /// </summary>
  701. /// <param name="currentFilename"></param>
  702. /// <param name="newFilename"></param>
  703. public static void MovieFile(string currentFilename, string newDirectory)
  704. {
  705. ReName(currentFilename, newDirectory);
  706. }
  707. public static string FileOrDirectory(string path)
  708. {
  709. if (File.Exists(path))
  710. {
  711. return "文件";
  712. }
  713. else if (Directory.Exists(path))
  714. {
  715. return "文件夹";
  716. }
  717. else
  718. {
  719. return "错误";
  720. }
  721. }
  722. }
  723. }

  

FTP创建与操作的更多相关文章

  1. ABP创建数据库操作步骤

    1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...

  2. cmd 下登陆ftp及相关操作

    cmd 下登陆ftp及相关操作 2011-08-09 20:34:28|  分类: 小技巧|字号 订阅 一.举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应 ...

  3. 第三百七十六节,Django+Xadmin打造上线标准的在线教育平台—创建用户操作app,在models.py文件生成5张表,用户咨询表、课程评论表、用户收藏表、用户消息表、用户学习表

    第三百七十六节,Django+Xadmin打造上线标准的在线教育平台—创建用户操作app,在models.py文件生成5张表,用户咨询表.课程评论表.用户收藏表.用户消息表.用户学习表 创建名称为ap ...

  4. c# ftp创建文件(非上传文件)

    c#  ftp创建文件(非上传文件) 一.奇葩的故事: 今天项目中遇到这么个奇葩的问题,ftp文件传输完成后要在ftp目录下另一个文件夹下创建对应的空文件,听说是为了文件的完整性,既然这么说,那么就必 ...

  5. R: vector 向量的创建、操作等。

    ################################################### 问题:创建.操作向量   18.4.27 怎么创建向量 vector,,及其相关操作 ??? 解 ...

  6. 基于commons-net实现ftp创建文件夹、上传、下载功能

    原文:http://www.open-open.com/code/view/1420774470187 package com.demo.ftp; import java.io.FileInputSt ...

  7. JavaScript面向对象—对象的创建和操作

    JavaScript面向对象-对象的创建和操作 前言 虽然说在JavaScript编程语言中,函数是第一公民,但是JavaScript不仅支持函数式编程,也支持面向对象编程.JavaScript对象设 ...

  8. 5. Git初始化及仓库创建和操作

    4. Git初始化及仓库创建和操作 基本信息设置 1. 设置用户名 git config --global user.name 'itcastphpgit1' 2. 设置用户名邮箱 git confi ...

  9. KVM虚拟机管理——虚拟机创建和操作系统安装

    1. 概述2. 交互式安装2.1 图形化-本地安装2.1.1 图形化本地CDROM安装2.2.2 图形化本地镜像安装2.2 命令行-本地安装2.2.1 命令行CDROM安装2.3 图形化-网络安装2. ...

随机推荐

  1. (转)IOS笔记 #pragma mark的用法

    简单的来说就是为了方便查找和导航代码用的.   下面举例如何快速的定位到我已经标识过的代码.     #pragma mark 播放节拍器 - (void) Run:(NSNumber *)tick{ ...

  2. canvas阴影

    shadowOffsetx 阴影X轴的移动 shadowOffsety 阴影的y轴移动 shadowColor 阴影颜色 shadowBlur 模糊范围 <!DOCTYPE html>&l ...

  3. 初学springMVC搭建框架过程及碰到的问题

    刚刚开始学spring框架,因为接了一个网站的项目,想用spring+springMVC+hibernate整合来实现它,现在写下搭建框架的过程及碰到的问题.希望给自己看到也能让大家看到不要踏坑. 一 ...

  4. MVC理解

    1:MVC 中的@是什么意思?   类似于<% %>只不过它没有闭合的,这是MVC3.0的新特性2:关于ASP.NET MVC的Html.BeginForm()方法Html.BeginFo ...

  5. C#:获取时间年月日时分秒格式

    //获取日期+时间 DateTime.Now.ToString();            // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToStri ...

  6. 经典:十步完全理解 SQL

    经典:十步完全理解 SQL   来源:伯乐在线 链接:http://blog.jobbole.com/55086/ 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完 ...

  7. php curl详解用法[真的详解]

    目前为目最全的CURL中文说明了,学PHP的要好好掌握.有很多的参数.大部份都很有用.真正掌握了它和正 则,一定就是个采集高手了. 通用函数: function curl_file_get_conte ...

  8. matlab差分算法

    今天实现了<一类求解方程全部根的改进差分进化算法>(by 宁桂英,周永权),虽然最后的实现结果并没有文中分析的那么好,但是本文依然是给了一个求解多项式全部实根的基本思路.思路是对的,利用了 ...

  9. Python核心编程读笔 10:函数和函数式编程

    第11章 函数和函数式编程 一 调用函数  1 关键字参数 def foo(x): foo_suite # presumably does some processing with 'x' 标准调用 ...

  10. QF——网络之知识碎片

    1.URL中文问题: URL不支持中文.若出现中文,需要对URL进行utf-8编码. NSString *urlString = [kULRSTRING stringByAddingPercentEs ...