FileHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 TEAMModelOS.SDK.Helper.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. var s = ex.Message;
  192. throw;
  193. }
  194. }
  195. /// <summary>
  196. /// 创建文件夹
  197. /// </summary>
  198. /// <param name="fileName">文件的绝对路径</param>
  199. public static void CreateFiles(string fileName)
  200. {
  201. try
  202. {
  203. //判断文件是否存在,不存在创建该文件
  204. if (!IsExistFile(fileName))
  205. {
  206. FileInfo file = new FileInfo(fileName);
  207. FileStream fs = file.Create();
  208. fs.Close();
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. throw ex;
  214. }
  215. }
  216. /// <summary>
  217. /// 创建一个文件,并将字节流写入文件。
  218. /// </summary>
  219. /// <param name="filePath">文件的绝对路径</param>
  220. /// <param name="buffer">二进制流数据</param>
  221. public static void CreateFile(string filePath, byte[] buffer)
  222. {
  223. try
  224. {
  225. //判断文件是否存在,不存在创建该文件
  226. if (!IsExistFile(filePath))
  227. {
  228. FileInfo file = new FileInfo(filePath);
  229. FileStream fs = file.Create();
  230. fs.Write(buffer, 0, buffer.Length);
  231. fs.Close();
  232. }
  233. else
  234. {
  235. File.WriteAllBytes(filePath, buffer);
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. throw ex;
  241. }
  242. }
  243. #endregion
  244. #region 将文件移动到指定目录
  245. /// <summary>
  246. /// 将文件移动到指定目录
  247. /// </summary>
  248. /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
  249. /// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
  250. public static void Move(string sourceFilePath, string descDirectoryPath)
  251. {
  252. string sourceName = GetFileName(sourceFilePath);
  253. if (IsExistDirectory(descDirectoryPath))
  254. {
  255. //如果目标中存在同名文件,则删除
  256. if (IsExistFile(descDirectoryPath + "\\" + sourceFilePath))
  257. {
  258. DeleteFile(descDirectoryPath + "\\" + sourceFilePath);
  259. }
  260. else
  261. {
  262. //将文件移动到指定目录
  263. File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFilePath);
  264. }
  265. }
  266. }
  267. #endregion
  268. # region 将源文件的内容复制到目标文件中
  269. /// <summary>
  270. /// 将源文件的内容复制到目标文件中
  271. /// </summary>
  272. /// <param name="sourceFilePath">源文件的绝对路径</param>
  273. /// <param name="descDirectoryPath">目标文件的绝对路径</param>
  274. public static void Copy(string sourceFilePath, string descDirectoryPath)
  275. {
  276. File.Copy(sourceFilePath, descDirectoryPath, true);
  277. }
  278. #endregion
  279. #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
  280. /// <summary>
  281. /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
  282. /// </summary>
  283. /// <param name="filePath">文件的绝对路径</param>
  284. public static string GetFileName(string filePath)
  285. {
  286. FileInfo file = new FileInfo(filePath);
  287. return file.Name;
  288. }
  289. #endregion
  290. #region 获取文件的后缀名
  291. /// <summary>
  292. /// 获取文件的后缀名
  293. /// </summary>
  294. /// <param name="filePath">文件的绝对路径</param>
  295. public static string GetExtension(string filePath)
  296. {
  297. FileInfo file = new FileInfo(filePath);
  298. return file.Extension;
  299. }
  300. /// <summary>
  301. /// 返回文件扩展名,不含“.”
  302. /// </summary>
  303. /// <param name="filepath">文件全名称</param>
  304. /// <returns>string</returns>
  305. public static string GetFileExt(string filepath)
  306. {
  307. if (string.IsNullOrEmpty(filepath))
  308. {
  309. return "";
  310. }
  311. if (filepath.LastIndexOf(".", StringComparison.Ordinal) > 0)
  312. {
  313. return filepath.Substring(filepath.LastIndexOf(".", StringComparison.Ordinal) + 1); //文件扩展名,不含“.”
  314. }
  315. return "";
  316. }
  317. #endregion
  318. #region 删除指定文件
  319. /// <summary>
  320. /// 删除指定文件
  321. /// </summary>
  322. /// <param name="filePath">文件的绝对路径</param>
  323. public static void DeleteFile(string filePath)
  324. {
  325. if (IsExistFile(filePath))
  326. {
  327. File.Delete(filePath);
  328. }
  329. }
  330. #endregion
  331. #region 删除指定目录及其所有子目录
  332. /// <summary>
  333. /// 删除指定目录及其所有子目录
  334. /// </summary>
  335. /// <param name="directoryPath">文件的绝对路径</param>
  336. public static void DeleteDirectory(string directoryPath)
  337. {
  338. if (IsExistDirectory(directoryPath))
  339. {
  340. Directory.Delete(directoryPath);
  341. }
  342. }
  343. #endregion
  344. #region 清空指定目录下所有文件及子目录,但该目录依然保存.
  345. /// <summary>
  346. /// 清空指定目录下所有文件及子目录,但该目录依然保存.
  347. /// </summary>
  348. /// <param name="directoryPath">指定目录的绝对路径</param>
  349. public static void ClearDirectory(string directoryPath)
  350. {
  351. if (!IsExistDirectory(directoryPath)) return;
  352. //删除目录中所有的文件
  353. string[] fileNames = GetFileNames(directoryPath);
  354. for (int i = 0; i < fileNames.Length; i++)
  355. {
  356. DeleteFile(fileNames[i]);
  357. }
  358. //删除目录中所有的子目录
  359. string[] directoryNames = GetDirectories(directoryPath);
  360. for (int i = 0; i < directoryNames.Length; i++)
  361. {
  362. DeleteDirectory(directoryNames[i]);
  363. }
  364. }
  365. #endregion
  366. #region 剪切 粘贴
  367. /// <summary>
  368. /// 剪切文件
  369. /// </summary>
  370. /// <param name="source">原路径</param>
  371. /// <param name="destination">新路径</param>
  372. public bool FileMove(string source, string destination)
  373. {
  374. bool ret = false;
  375. FileInfo file_s = new FileInfo(source);
  376. FileInfo file_d = new FileInfo(destination);
  377. if (file_s.Exists)
  378. {
  379. if (!file_d.Exists)
  380. {
  381. file_s.MoveTo(destination);
  382. ret = true;
  383. }
  384. }
  385. if (ret == true)
  386. {
  387. //Response.Write("<script>alert('剪切文件成功!');</script>");
  388. }
  389. else
  390. {
  391. //Response.Write("<script>alert('剪切文件失败!');</script>");
  392. }
  393. return ret;
  394. }
  395. #endregion
  396. #region 检测指定目录是否为空
  397. /// <summary>
  398. /// 检测指定目录是否为空
  399. /// </summary>
  400. /// <param name="directoryPath">指定目录的绝对路径</param>
  401. public static bool IsEmptyDirectory(string directoryPath)
  402. {
  403. try
  404. {
  405. //判断文件是否存在
  406. string[] fileNames = GetFileNames(directoryPath);
  407. if (fileNames.Length > 0)
  408. {
  409. return false;
  410. }
  411. //判断是否存在文件夹
  412. string[] directoryNames = GetDirectories(directoryPath);
  413. if (directoryNames.Length > 0)
  414. {
  415. return false;
  416. }
  417. return true;
  418. }
  419. catch (Exception ex)
  420. {
  421. var s = ex.Message;
  422. return true;
  423. }
  424. }
  425. #endregion
  426. #region 获取指定目录中所有文件列表
  427. /// <summary>
  428. /// 获取指定目录中所有文件列表
  429. /// </summary>
  430. /// <param name="directoryPath">指定目录的绝对路径</param>
  431. public static string[] GetFileNames(string directoryPath)
  432. {
  433. if (!IsExistDirectory(directoryPath))
  434. {
  435. throw new FileNotFoundException();
  436. }
  437. return Directory.GetFiles(directoryPath);
  438. }
  439. #endregion
  440. #region 获取指定目录中的子目录列表
  441. /// <summary>
  442. /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法
  443. /// </summary>
  444. /// <param name="directoryPath">指定目录的绝对路径</param>
  445. public static string[] GetDirectories(string directoryPath)
  446. {
  447. try
  448. {
  449. return Directory.GetDirectories(directoryPath);
  450. }
  451. catch (Exception ex)
  452. {
  453. throw ex;
  454. }
  455. }
  456. /// <summary>
  457. /// 获取指定目录及子目录中所有子目录列表
  458. /// </summary>
  459. /// <param name="directoryPath">指定目录的绝对路径</param>
  460. /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
  461. /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
  462. /// <param name="isSearchChild">是否搜索子目录</param>
  463. public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
  464. {
  465. try
  466. {
  467. if (isSearchChild)
  468. {
  469. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories);
  470. }
  471. else
  472. {
  473. return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
  474. }
  475. }
  476. catch (Exception ex)
  477. {
  478. throw ex;
  479. }
  480. }
  481. #endregion
  482. #region 获取一个文件的长度
  483. /// <summary>
  484. /// 获取一个文件的长度,单位为Byte
  485. /// </summary>
  486. /// <param name="filePath">文件的绝对路径</param>
  487. public static int GetFileSize(string filePath)
  488. {
  489. //创建一个文件对象
  490. FileInfo fi = new FileInfo(filePath);
  491. //获取文件的大小
  492. return (int)fi.Length;
  493. }
  494. /// <summary>
  495. /// 获取一个文件的长度,单位为KB
  496. /// </summary>
  497. /// <param name="filePath">文件的路径</param>
  498. public static double GetFileSizeByKb(string filePath)
  499. {
  500. //创建一个文件对象
  501. FileInfo fi = new FileInfo(filePath);
  502. //获取文件的大小
  503. return Math.Round(Convert.ToDouble(filePath.Length) / 1024, 2);// ConvertHelper.ToDouble(ConvertHelper.ToDouble(fi.Length) / 1024, 1);
  504. }
  505. /// <summary>
  506. /// 获取一个文件的长度,单位为MB
  507. /// </summary>
  508. /// <param name="filePath">文件的路径</param>
  509. public static double GetFileSizeByMb(string filePath)
  510. {
  511. //创建一个文件对象
  512. FileInfo fi = new FileInfo(filePath);
  513. //获取文件的大小
  514. return Math.Round(Convert.ToDouble(Convert.ToDouble(fi.Length) / 1024 / 1024), 2);
  515. }
  516. #endregion
  517. #region 获取所有文件夹及子文件夹
  518. /// <summary>
  519. /// 获取所有文件夹及子文件夹
  520. /// </summary>
  521. /// <param name="dirPath"></param>
  522. /// <returns></returns>
  523. public static List<ArrayFiles> GetDirs(string dirPath)
  524. {
  525. var list = new List<ArrayFiles>();
  526. return GetArrys(dirPath, 0, list);
  527. }
  528. private static int zdId = 0;
  529. private static List<ArrayFiles> GetArrys(string dirPath, int pid, List<ArrayFiles> list)
  530. {
  531. if (!Directory.Exists(dirPath)) return list;
  532. if (Directory.GetDirectories(dirPath).Length <= 0) return list;
  533. foreach (string path in Directory.GetDirectories(dirPath))
  534. {
  535. zdId++;
  536. var model = new ArrayFiles()
  537. {
  538. Id = zdId,
  539. Name = path.Substring(path.LastIndexOf('\\') + 1, path.Length - (path.LastIndexOf('\\') + 1)),
  540. Pid = pid,
  541. Open = false,
  542. Target = "DeployBase",
  543. Url = "/FytAdmin/FileMiam/DocList?path=" + path
  544. };
  545. list.Add(model);
  546. GetArrys(path, model.Id, list);
  547. }
  548. return list;
  549. }
  550. public class ArrayFiles
  551. {
  552. public int Id { get; set; }
  553. public int Pid { get; set; }
  554. public string Name { get; set; }
  555. public Boolean Open { get; set; }
  556. public string Target { get; set; }
  557. public string Url { get; set; }
  558. }
  559. #endregion
  560. #region 将文件读取到字符串中
  561. /// <summary>
  562. /// 将文件读取到字符串中
  563. /// </summary>
  564. /// <param name="filePath">文件的绝对路径</param>
  565. public static string FileToString(string filePath)
  566. {
  567. return FileToString(filePath, Encoding.UTF8);
  568. }
  569. /// <summary>
  570. /// 将文件读取到字符串中
  571. /// </summary>
  572. /// <param name="filePath">文件的绝对路径</param>
  573. /// <param name="encoding">字符编码</param>
  574. public static string FileToString(string filePath, Encoding encoding)
  575. {
  576. //创建流读取器
  577. StreamReader reader = new StreamReader(filePath, encoding);
  578. try
  579. {
  580. //读取流
  581. return reader.ReadToEnd();
  582. }
  583. catch (Exception ex)
  584. {
  585. throw ex;
  586. }
  587. finally
  588. {
  589. //关闭流读取器
  590. reader.Close();
  591. }
  592. }
  593. #endregion
  594. }
  595. /// <summary>
  596. /// 远程文件下载
  597. /// </summary>
  598. public class HttpDldFile
  599. {
  600. /// <summary>
  601. /// Http方式下载文件
  602. /// </summary>
  603. /// <param name="url">http地址</param>
  604. /// <param name="localfile">本地文件</param>
  605. /// <returns></returns>
  606. public bool Download(string url, string localfile)
  607. {
  608. bool flag = false;
  609. long startPosition = 0; // 上次下载的文件起始位置
  610. FileStream writeStream; // 写入本地文件流对象
  611. long remoteFileLength = GetHttpLength(url);// 取得远程文件长度
  612. //System.Console.WriteLine("remoteFileLength=" + remoteFileLength);
  613. if (remoteFileLength == 745)
  614. {
  615. System.Console.WriteLine("远程文件不存在.");
  616. return false;
  617. }
  618. // 判断要下载的文件夹是否存在
  619. if (File.Exists(localfile))
  620. {
  621. writeStream = File.OpenWrite(localfile); // 存在则打开要下载的文件
  622. startPosition = writeStream.Length; // 获取已经下载的长度
  623. if (startPosition >= remoteFileLength)
  624. {
  625. System.Console.WriteLine("本地文件长度" + startPosition + "已经大于等于远程文件长度" + remoteFileLength);
  626. writeStream.Close();
  627. return false;
  628. }
  629. else
  630. {
  631. writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件写入位置定位
  632. }
  633. }
  634. else
  635. {
  636. writeStream = new FileStream(localfile, FileMode.Create);// 文件不保存创建一个文件
  637. startPosition = 0;
  638. }
  639. try
  640. {
  641. HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
  642. if (startPosition > 0)
  643. {
  644. myRequest.AddRange((int)startPosition);// 设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置
  645. }
  646. Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流
  647. byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容
  648. int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向远程文件读第一次
  649. long currPostion = startPosition;
  650. while (contentSize > 0)// 如果读取长度大于零则继续读
  651. {
  652. currPostion += contentSize;
  653. int percent = (int)(currPostion * 100 / remoteFileLength);
  654. System.Console.WriteLine("percent=" + percent + "%");
  655. writeStream.Write(btArray, 0, contentSize);// 写入本地文件
  656. contentSize = readStream.Read(btArray, 0, btArray.Length);// 继续向远程文件读取
  657. }
  658. //关闭流
  659. writeStream.Close();
  660. readStream.Close();
  661. flag = true; //返回true下载成功
  662. }
  663. catch (Exception)
  664. {
  665. writeStream.Close();
  666. flag = false; //返回false下载失败
  667. }
  668. return flag;
  669. }
  670. // 从文件头得到远程文件的长度
  671. private static long GetHttpLength(string url)
  672. {
  673. long length = 0;
  674. try
  675. {
  676. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
  677. HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
  678. if (rsp.StatusCode == HttpStatusCode.OK)
  679. {
  680. length = rsp.ContentLength;// 从文件头得到远程文件的长度
  681. }
  682. rsp.Close();
  683. return length;
  684. }
  685. catch (Exception e)
  686. {
  687. var s = e.Message;
  688. return length;
  689. }
  690. }
  691. }
  692. }