当前位置:网站首页>判断网络文件是否存在,获取网络文件大小,创建时间、修改时间
判断网络文件是否存在,获取网络文件大小,创建时间、修改时间
2022-06-12 11:33:00 【scimence】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace Sci
{
/// <summary>
/// 网络文件处理工具
/// </summary>
class WebFile
{
/// <summary>
/// 判断网络文件是否存在
/// </summary>
/// <param name="url"></param>
public static bool fileExist(string url)
{
return getFileLen(url) != -1;
}
/// <summary>
/// 获取网络文件大小
/// </summary>
/// <param name="url">文件网址</param>
/// <returns></returns>
public static long getFileLen(string url)
{
try
{
WebRequest request = WebRequest.Create(url);
request.Timeout = 200;
WebResponse response = request.GetResponse();
return response.ContentLength; // 文件大小
//return response.ContentType; // 文件类型
}
catch (Exception)
{
return -1; // 文件不存在
}
}
/// [Accept-Ranges, bytes]
/// [Content-Length, 13927]
/// [Content-Type, application/x-zip-compressed]
/// [Date, 2022-05-28 12:32:59]
/// [ETag, "9258b93b05ed81:0"]
/// [Last-Modified, 2022-05-02 16:40:10]
/// [Server, Microsoft-IIS/8.5]
/// [X-Powered-By, ASP.NET]
/// <summary>
/// 获取网络文件属性信息,如:getFileInfo(url)["Last-Modified"];
/// </summary>
/// <param name="url">文件网址</param>
/// <returns></returns>
public static Dictionary<string, string> getFileInfo(string url)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
try
{
WebRequest request = WebRequest.Create(url);
request.Timeout = 200;
WebResponse response = request.GetResponse();
WebHeaderCollection col = response.Headers;
foreach (string key in col.Keys)
{
string value = col.Get(key);
if (value.EndsWith(" GMT")) value = DateTime.Parse(value).ToString("yyyy-MM-dd HH:mm:ss");
if (!dic.ContainsKey(key)) dic.Add(key, value);
}
}
catch (Exception)
{
}
return dic;
}
}
}
边栏推荐
- VirtualBox 虚拟机因系统异常关机虚拟机启动项不见了
- 2022-06-11:注意本文件中,graph不是邻接矩阵的含义,而是一个二部图。 在长度为N的邻接矩阵matrix中,所有的点有N个,matrix[i][j]
- Record the pits encountered when using JPA
- ^33 variable promotion and function promotion interview questions
- go基于阿里云实现发送短信
- Epidemic home office experience | community essay solicitation
- arm各种交叉编译工具的区别
- MATLAB中stairs函数使用
- 6.6 分离卷积
- Network topology
猜你喜欢

K52. Chapter 1: installing kubernetes v1.22 based on kubeadm -- cluster deployment

B+ 树的简单认识

Face recognition PIP failed to install Dlib Library

Relatively rare exception records in UI automation test

你需要社交媒体二维码的21个理由

VirtualBox 虚拟机因系统异常关机虚拟机启动项不见了

多普勒效应的基本原理

Pytoch notes

C# 37. textbox滚动条与多行

Clickhouse column basic data type description
随机推荐
The evil 203 in systemctl
MySQL45讲 01 | 基础架构:一条SQL查询语句是如何执行的?
NLP data set download address for naturallanguageprocessing
AcWing 128. Editor (to effectively modify the specified position in the top stack)
K53. Chapter 2 installing kubernetes v1.22 based on binary packages -- cluster deployment
M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302
DS18B20数字温度计 (一) 电气特性, 供电和接线方式
go基于腾讯云实现发送短信
Clickhouse column basic data type description
如何查看glibc版本
Byte order (network / host) conversion
^33变量提升和函数提升面试题
Arm cross compilation chain download address
人類想要擁有金錢、權力、美麗、永生、幸福……但海龜只想做一只海龜
systemctl里万恶的203
十折交叉验证代码中的问题
Unity 连接 Microsoft SQLSERVER 数据库
Index in MySQL show index from XXX the meaning of each parameter
Postman incoming list
Lambda expression | shallow solution