FileHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. namespace HaBookCms.Common.FileHelper
  8. {
  9. public class FileHelper
  10. {
  11. #region 获取文件到集合中
  12. /// <summary>
  13. /// 读取指定位置文件列表到集合中
  14. /// </summary>
  15. /// <param name="path">指定路径</param>
  16. /// <returns></returns>
  17. public static DataTable GetFileTable(string path)
  18. {
  19. DataTable dt = new DataTable();
  20. dt.Columns.Add("name", typeof(string));
  21. dt.Columns.Add("ext", typeof(string));
  22. dt.Columns.Add("size", typeof(long));
  23. dt.Columns.Add("time", typeof(DateTime));
  24. DirectoryInfo dirinfo = new DirectoryInfo(path);
  25. FileInfo fi;
  26. DirectoryInfo dir;
  27. string FileName, FileExt;
  28. long FileSize = 0;
  29. DateTime FileModify;
  30. try
  31. {
  32. foreach (FileSystemInfo fsi in dirinfo.GetFileSystemInfos())
  33. {
  34. FileName = string.Empty;
  35. FileExt = string.Empty;
  36. if (fsi is FileInfo)
  37. {
  38. fi = (FileInfo)fsi;
  39. //获取文件名称
  40. FileName = fi.Name;
  41. //获取文件扩展名
  42. FileExt = fi.Extension;
  43. //获取文件大小
  44. FileSize = fi.Length;
  45. //获取文件最后修改时间
  46. FileModify = fi.LastWriteTime;
  47. }
  48. else
  49. {
  50. dir = (DirectoryInfo)fsi;
  51. //获取目录名
  52. FileName = dir.Name;
  53. //获取目录最后修改时间
  54. FileModify = dir.LastWriteTime;
  55. //设置目录文件为文件夹
  56. FileExt = "文件夹";
  57. }
  58. DataRow dr = dt.NewRow();
  59. dr["name"] = FileName;
  60. dr["ext"] = FileExt;
  61. dr["size"] = FileSize;
  62. dr["time"] = FileModify;
  63. dt.Rows.Add(dr);
  64. }
  65. }
  66. catch
  67. {
  68. throw;
  69. }
  70. return dt;
  71. }
  72. #endregion
  73. #region 检测指定路径是否存在
  74. /// <summary>
  75. /// 检测指定路径是否存在
  76. /// </summary>
  77. /// <param name="path">目录的绝对路径</param>
  78. public static bool IsExistDirectory(string path)
  79. {
  80. return Directory.Exists(path);
  81. }
  82. #endregion
  83. #region 检测指定文件是否存在,如果存在则返回true
  84. /// <summary>
  85. /// 检测指定文件是否存在,如果存在则返回true
  86. /// </summary>
  87. /// <param name="filePath">文件的绝对路径</param>
  88. public static bool IsExistFile(string filePath)
  89. {
  90. return File.Exists(filePath);
  91. }
  92. #endregion
  93. #region 创建文件夹
  94. /// <summary>
  95. /// 创建文件夹
  96. /// </summary>
  97. /// <param name="folderPath">文件夹的绝对路径</param>
  98. public static void CreateFolder(string folderPath)
  99. {
  100. if (!IsExistDirectory(folderPath))
  101. {
  102. Directory.CreateDirectory(folderPath);
  103. }
  104. }
  105. #endregion
  106. #region 判断上传文件后缀名
  107. /// <summary>
  108. /// 判断上传文件后缀名
  109. /// </summary>
  110. /// <param name="strExtension">后缀名</param>
  111. public static bool IsCanEdit(string strExtension)
  112. {
  113. strExtension = strExtension.ToLower();
  114. if (strExtension.LastIndexOf(".", StringComparison.Ordinal) >= 0)
  115. {
  116. strExtension = strExtension.Substring(strExtension.LastIndexOf(".", StringComparison.Ordinal));
  117. }
  118. else
  119. {
  120. strExtension = ".txt";
  121. }
  122. string[] strArray = new string[] { ".htm", ".html", ".txt", ".js", ".css", ".xml", ".sitemap" };
  123. for (int i = 0; i < strArray.Length; i++)
  124. {
  125. if (strExtension.Equals(strArray[i]))
  126. {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. public static bool IsSafeName(string strExtension)
  133. {
  134. strExtension = strExtension.ToLower();
  135. if (strExtension.LastIndexOf(".") >= 0)
  136. {
  137. strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
  138. }
  139. else
  140. {
  141. strExtension = ".txt";
  142. }
  143. string[] strArray = new string[] { ".jpg", ".gif", ".png" };
  144. for (int i = 0; i < strArray.Length; i++)
  145. {
  146. if (strExtension.Equals(strArray[i]))
  147. {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. public static bool IsZipName(string strExtension)
  154. {
  155. strExtension = strExtension.ToLower();
  156. if (strExtension.LastIndexOf(".") >= 0)
  157. {
  158. strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
  159. }
  160. else
  161. {
  162. strExtension = ".txt";
  163. }
  164. string[] strArray = new string[] { ".zip", ".rar" };
  165. for (int i = 0; i < strArray.Length; i++)
  166. {
  167. if (strExtension.Equals(strArray[i]))
  168. {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. #endregion
  175. #region 创建文件夹
  176. /// <summary>
  177. /// 创建文件夹
  178. /// </summary>
  179. /// <param name="fileName">文件的绝对路径</param>
  180. public static void CreateSuffic(string fileName)
  181. {
  182. try
  183. {
  184. if (!Directory.Exists(fileName))
  185. {
  186. Directory.CreateDirectory(fileName);
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. throw;
  192. }
  193. }
  194. /// <summary>
  195. /// 创建文件夹
  196. /// </summary>
  197. /// <param name="fileName">文件的绝对路径</param>
  198. public static void CreateFiles(string fileName)
  199. {
  200. try
  201. {
  202. //判断文件是否存在,不存在创建该文件
  203. if (!IsExistFile(fileName))
  204. {
  205. FileInfo file = new FileInfo(fileName);
  206. FileStream fs = file.Create();
  207. fs.Close();
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. throw ex;
  213. }
  214. }
  215. /// <summary>
  216. /// 创建一个文件,并将字节流写入文件。
  217. /// </summary>
  218. /// <param name="filePath">文件的绝对路径</param>
  219. /// <param name="buffer">二进制流数据</param>
  220. public static void CreateFile(string filePath, byte[] buffer)
  221. {
  222. try
  223. {
  224. //判断文件是否存在,不存在创建该文件
  225. if (!IsExistFile(filePath))
  226. {
  227. FileInfo file = new FileInfo(filePath);
  228. FileStream fs = file.Create();
  229. fs.Write(buffer, 0, buffer.Length);
  230. fs.Close();
  231. }
  232. else
  233. {
  234. File.WriteAllBytes(filePath, buffer);
  235. }
  236. }
  237. catch (Exception ex)
  238. {
  239. throw ex;
  240. }
  241. }
  242. #endregion
  243. #region 将文件移动到指定目录
  244. /// <summary>
  245. /// 将文件移动到指定目录
  246. /// </summary>
  247. /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
  248. /// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
  249. public static void Move(string sourceFilePath, string descDirectoryPath)
  250. {
  251. string sourceName = GetFileName(sourceFilePath);
  252. if (IsExistDirectory(descDirectoryPath))
  253. {
  254. //如果目标中存在同名文件,则删除
  255. if (IsExistFile(descDirectoryPath + "\\" + sourceFilePath))
  256. {
  257. DeleteFile(descDirectoryPath + "\\" + sourceFilePath);
  258. }
  259. else
  260. {
  261. //将文件移动到指定目录
  262. File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFilePath);
  263. }
  264. }
  265. }
  266. #endregion
  267. # region 将源文件的内容复制到目标文件中
  268. /// <summary>
  269. /// 将源文件的内容复制到目标文件中
  270. /// </summary>
  271. /// <param name="sourceFilePath">源文件的绝对路径</param>
  272. /// <param name="descDirectoryPath">目标文件的绝对路径</param>
  273. public static void Copy(string sourceFilePath, string descDirectoryPath)
  274. {
  275. File.Copy(sourceFilePath, descDirectoryPath, true);
  276. }
  277. #endregion
  278. #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
  279. /// <summary>
  280. /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
  281. /// </summary>
  282. /// <param name="filePath">文件的绝对路径</param>
  283. public static string GetFileName(string filePath)
  284. {
  285. FileInfo file = new FileInfo(filePath);
  286. return file.Name;
  287. }
  288. #endregion
  289. #region 获取文件的后缀名
  290. /// <summary>
  291. /// 获取文件的后缀名
  292. /// </summary>
  293. /// <param name="filePath">文件的绝对路径</param>
  294. public static string GetExtension(string filePath)
  295. {
  296. FileInfo file = new FileInfo(filePath);
  297. return file.Extension;
  298. }
  299. /// <summary>
  300. /// 返回文件扩展名,不含“.”
  301. /// </summary>
  302. /// <param name="filepath">文件全名称</param>
  303. /// <returns>string</returns>
  304. public static string GetFileExt(string filepath)
  305. {
  306. if (string.IsNullOrEmpty(filepath))
  307. {
  308. return "";
  309. }
  310. if (filepath.LastIndexOf(".", StringComparison.Ordinal) > 0)
  311. {
  312. return filepath.Substring(filepath.LastIndexOf(".", StringComparison.Ordinal) + 1); //文件扩展名,不含“.”
  313. }
  314. return "";
  315. }
  316. #endregion
  317. #region 删除指定文件
  318. /// <summary>
  319. /// 删除指定文件
  320. /// </summary>
  321. /// <param name="filePath">文件的绝对路径</param>
  322. public static void DeleteFile(string filePath)
  323. {
  324. if (IsExistFile(filePath))
  325. {
  326. File.Delete(filePath);
  327. }
  328. }
  329. #endregion
  330. #region 删除指定目录及其所有子目录
  331. /// <summary>
  332. /// 删除指定目录及其所有子目录
  333. /// </summary>
  334. /// <param name="directoryPath">文件的绝对路径</param>
  335. public static void DeleteDirectory(string directoryPath)
  336. {
  337. if (IsExistDirectory(directoryPath))
  338. {
  339. Directory.Delete(directoryPath);
  340. }
  341. }
  342. #endregion
  343. #region 清空指定目录下所有文件及子目录,但该目录依然保存.
  344. /// <summary>
  345. /// 清空指定目录下所有文件及子目录,但该目录依然保存.
  346. /// </summary>
  347. /// <param name="directoryPath">指定目录的绝对路径</param>
  348. public static void ClearDirectory(string directoryPath)
  349. {
  350. if (!IsExistDirectory(directoryPath)) return;
  351. //删除目录中所有的文件
  352. string[] fileNames = GetFileNames(directoryPath);
  353. for (int i = 0; i < fileNames.Length; i++)
  354. {
  355. DeleteFile(fileNames[i]);
  356. }
  357. //删除目录中所有的子目录
  358. string[] directoryNames = GetDirectories(directoryPath);
  359. for (int i = 0; i < directoryNames.Length; i++)
  360. {
  361. DeleteDirectory(directoryNames[i]);
  362. }
  363. }
  364. #endregion
  365. #region 剪切 粘贴
  366. /// <summary>
  367. /// 剪切文件
  368. /// </summary>
  369. /// <param name="source">原路径</param>
  370. /// <param name="destination">新路径</param>
  371. public bool FileMove(string source, string destination)
  372. {
  373. bool ret = false;
  374. FileInfo file_s = new FileInfo(source);
  375. FileInfo file_d = new FileInfo(destination);
  376. if (file_s.Exists)
  377. {
  378. if (!file_d.Exists)
  379. {
  380. file_s.MoveTo(destination);
  381. ret = true;
  382. }
  383. }
  384. if (ret == true)
  385. {
  386. //Response.Write("<script>alert('剪切文件成功!');</script>");
  387. }
  388. else
  389. {
  390. //Response.Write("<script>alert('剪切文件失败!');</script>");
  391. }
  392. return ret;
  393. }
  394. #endregion
  395. #region 检测指定目录是否为空
  396. /// <summary>
  397. /// 检测指定目录是否为空
  398. /// </summary>
  399. /// <param name="directoryPath">指定目录的绝对路径</param>
  400. public static bool IsEmptyDirectory(string directoryPath)
  401. {
  402. try
  403. {
  404. //判断文件是否存在
  405. string[] fileNames = GetFileNames(directoryPath);
  406. if (fileNames.Length > 0)
  407. {
  408. return false;
  409. }
  410. //判断是否存在文件夹
  411. string[] directoryNames = GetDirectories(directoryPath);
  412. if (directoryNames.Length > 0)
  413. {
  414. return false;
  415. }
  416. return true;
  417. }
  418. catch (Exception ex)
  419. {
  420. return true;
  421. }
  422. }
  423. #endregion
  424. #region 获取指定目录中所有文件列表
  425. /// <summary>
  426. /// 获取指定目录中所有文件列表
  427. /// </summary>
  428. /// <param name="directoryPath">指定目录的绝对路径</param>
  429. public static string[] GetFileNames(string directoryPath)
  430. {
  431. if (!IsExistDirectory(directoryPath))
  432. {
  433. throw new FileNotFoundException();
  434. }
  435. return Directory.GetFiles(directoryPath);
  436. }
  437. #endregion
  438. #region 获取指定目录中的子目录列表
  439. /// <summary>
  440. /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法
  441. /// </summary>
  442. /// <param name="directoryPath">指定目录的绝对路径</param>
  443. public static string[] GetDirectories(string directoryPath)
  444. {
  445. try
  446. {
  447. return Directory.GetDirectories(directoryPath);
  448. }
  449. catch (Exception ex)
  450. {
  451. throw ex;
  452. }
  453. }
  454. /// <summary>
  455. /// 获取指定目录及子目录中所有子目录列表
  456. /// </summary>
  457. /// <param name="directoryPath">指定目录的绝对路径</param>
  458. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  459. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  460. /// <param name="isSearchChild">是否搜索子目录</param>
  461. public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
  462. {
  463. try
  464. {
  465. if (isSearchChild)
  466. {
  467. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories);
  468. }
  469. else
  470. {
  471. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
  472. }
  473. }
  474. catch (Exception ex)
  475. {
  476. throw ex;
  477. }
  478. }
  479. #endregion
  480. #region 获取一个文件的长度
  481. /// <summary>
  482. /// 获取一个文件的长度,单位为Byte
  483. /// </summary>
  484. /// <param name="filePath">文件的绝对路径</param>
  485. public static int GetFileSize(string filePath)
  486. {
  487. //创建一个文件对象
  488. FileInfo fi = new FileInfo(filePath);
  489. //获取文件的大小
  490. return (int)fi.Length;
  491. }
  492. /// <summary>
  493. /// 获取一个文件的长度,单位为KB
  494. /// </summary>
  495. /// <param name="filePath">文件的路径</param>
  496. public static double GetFileSizeByKb(string filePath)
  497. {
  498. //创建一个文件对象
  499. FileInfo fi = new FileInfo(filePath);
  500. //获取文件的大小
  501. return Math.Round(Convert.ToDouble(filePath.Length) / 1024, 2);// ConvertHelper.ToDouble(ConvertHelper.ToDouble(fi.Length) / 1024, 1);
  502. }
  503. /// <summary>
  504. /// 获取一个文件的长度,单位为MB
  505. /// </summary>
  506. /// <param name="filePath">文件的路径</param>
  507. public static double GetFileSizeByMb(string filePath)
  508. {
  509. //创建一个文件对象
  510. FileInfo fi = new FileInfo(filePath);
  511. //获取文件的大小
  512. return Math.Round(Convert.ToDouble(Convert.ToDouble(fi.Length) / 1024 / 1024), 2);
  513. }
  514. #endregion
  515. #region 获取所有文件夹及子文件夹
  516. /// <summary>
  517. /// 获取所有文件夹及子文件夹
  518. /// </summary>
  519. /// <param name="dirPath"></param>
  520. /// <returns></returns>
  521. public static List<ArrayFiles> GetDirs(string dirPath)
  522. {
  523. var list = new List<ArrayFiles>();
  524. return GetArrys(dirPath, 0, list);
  525. }
  526. private static int zdId = 0;
  527. private static List<ArrayFiles> GetArrys(string dirPath, int pid, List<ArrayFiles> list)
  528. {
  529. if (!Directory.Exists(dirPath)) return list;
  530. if (Directory.GetDirectories(dirPath).Length <= 0) return list;
  531. foreach (string path in Directory.GetDirectories(dirPath))
  532. {
  533. zdId++;
  534. var model = new ArrayFiles()
  535. {
  536. id = zdId,
  537. name = path.Substring(path.LastIndexOf('\\') + 1, path.Length - (path.LastIndexOf('\\') + 1)),
  538. pId = pid,
  539. open = false,
  540. target = "DeployBase",
  541. url = "/FytAdmin/FileMiam/DocList?path=" + path
  542. };
  543. list.Add(model);
  544. GetArrys(path, model.id, list);
  545. }
  546. return list;
  547. }
  548. public class ArrayFiles
  549. {
  550. public int id { get; set; }
  551. public int pId { get; set; }
  552. public string name { get; set; }
  553. public Boolean open { get; set; }
  554. public string target { get; set; }
  555. public string url { get; set; }
  556. }
  557. #endregion
  558. #region 将文件读取到字符串中
  559. /// <summary>
  560. /// 将文件读取到字符串中
  561. /// </summary>
  562. /// <param name="filePath">文件的绝对路径</param>
  563. public static string FileToString(string filePath)
  564. {
  565. return FileToString(filePath, Encoding.UTF8);
  566. }
  567. /// <summary>
  568. /// 将文件读取到字符串中
  569. /// </summary>
  570. /// <param name="filePath">文件的绝对路径</param>
  571. /// <param name="encoding">字符编码</param>
  572. public static string FileToString(string filePath, Encoding encoding)
  573. {
  574. //创建流读取器
  575. StreamReader reader = new StreamReader(filePath, encoding);
  576. try
  577. {
  578. //读取流
  579. return reader.ReadToEnd();
  580. }
  581. catch (Exception ex)
  582. {
  583. throw ex;
  584. }
  585. finally
  586. {
  587. //关闭流读取器
  588. reader.Close();
  589. }
  590. }
  591. #endregion
  592. }
  593. /// <summary>
  594. /// 远程文件下载
  595. /// </summary>
  596. public class HttpDldFile
  597. {
  598. /// <summary>
  599. /// Http方式下载文件
  600. /// </summary>
  601. /// <param name="url">http地址</param>
  602. /// <param name="localfile">本地文件</param>
  603. /// <returns></returns>
  604. public bool Download(string url, string localfile)
  605. {
  606. bool flag = false;
  607. long startPosition = 0; // 上次下载的文件起始位置
  608. FileStream writeStream; // 写入本地文件流对象
  609. long remoteFileLength = GetHttpLength(url);// 取得远程文件长度
  610. //System.Console.WriteLine("remoteFileLength=" + remoteFileLength);
  611. if (remoteFileLength == 745)
  612. {
  613. System.Console.WriteLine("远程文件不存在.");
  614. return false;
  615. }
  616. // 判断要下载的文件夹是否存在
  617. if (File.Exists(localfile))
  618. {
  619. writeStream = File.OpenWrite(localfile); // 存在则打开要下载的文件
  620. startPosition = writeStream.Length; // 获取已经下载的长度
  621. if (startPosition >= remoteFileLength)
  622. {
  623. System.Console.WriteLine("本地文件长度" + startPosition + "已经大于等于远程文件长度" + remoteFileLength);
  624. writeStream.Close();
  625. return false;
  626. }
  627. else
  628. {
  629. writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件写入位置定位
  630. }
  631. }
  632. else
  633. {
  634. writeStream = new FileStream(localfile, FileMode.Create);// 文件不保存创建一个文件
  635. startPosition = 0;
  636. }
  637. try
  638. {
  639. HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
  640. if (startPosition > 0)
  641. {
  642. myRequest.AddRange((int)startPosition);// 设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置
  643. }
  644. Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流
  645. byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容
  646. int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向远程文件读第一次
  647. long currPostion = startPosition;
  648. while (contentSize > 0)// 如果读取长度大于零则继续读
  649. {
  650. currPostion += contentSize;
  651. int percent = (int)(currPostion * 100 / remoteFileLength);
  652. System.Console.WriteLine("percent=" + percent + "%");
  653. writeStream.Write(btArray, 0, contentSize);// 写入本地文件
  654. contentSize = readStream.Read(btArray, 0, btArray.Length);// 继续向远程文件读取
  655. }
  656. //关闭流
  657. writeStream.Close();
  658. readStream.Close();
  659. flag = true; //返回true下载成功
  660. }
  661. catch (Exception)
  662. {
  663. writeStream.Close();
  664. flag = false; //返回false下载失败
  665. }
  666. return flag;
  667. }
  668. // 从文件头得到远程文件的长度
  669. private static long GetHttpLength(string url)
  670. {
  671. long length = 0;
  672. try
  673. {
  674. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
  675. HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
  676. if (rsp.StatusCode == HttpStatusCode.OK)
  677. {
  678. length = rsp.ContentLength;// 从文件头得到远程文件的长度
  679. }
  680. rsp.Close();
  681. return length;
  682. }
  683. catch (Exception e)
  684. {
  685. return length;
  686. }
  687. }
  688. }
  689. }