zoukankan      html  css  js  c++  java
  • C#判断本地文件,网络文件是否存在是否存在

    1、判断本地文件是否存在代码:

    复制代码
            static void Main(string[] args)
            {
    string path = "C:/Users/lenovo/Desktop/test.jpg"; if (System.IO.File.Exists(path)) { Console.WriteLine("本地文件确实存在!"); } else { Console.WriteLine("本地文件不存在!"); } Console.ReadKey(); }
    复制代码

    主要是通过System.IO.FIle对象的Exists方法来进行判断。

    2、判断网络文件是否存在代码:

    网络地址→请求对象→判断响应状态是否为200。

    复制代码
            static void Main(string[] args)
            {
                string url = @"https://www.baidu.com/test.png";//网络文件地址
                if (JudgeFileExist(url))
                { Console.WriteLine("网络文件确实存在!"); }
                else
                { Console.WriteLine("网络文件不存在!"); }
                Console.ReadKey();
            }
            private static bool JudgeFileExist(string url)
            {
                try
                {
                    //创建根据网络地址的请求对象
                    System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.CreateDefault(new Uri(url));
                    httpWebRequest.Method = "HEAD";
                    httpWebRequest.Timeout = 1000;
                    //返回响应状态是否是成功比较的布尔值
                    return (((System.Net.HttpWebResponse)httpWebRequest.GetResponse()).StatusCode == System.Net.HttpStatusCode.OK);
                }
                catch
                {
                    return false;
                }
            }
  • 相关阅读:
    random,time,os
    内置函数
    迭代器,生成器,开放封闭原则,列表推导式
    函数的有用信息
    装饰器
    动态参数,作用域,闭包
    初始函数def
    python之文件操作
    “SLR”指人造卫星激光测距,“VLBI”指甚长基线干涉测量。
    解压软件使用方法
  • 原文地址:https://www.cnblogs.com/vaevvaev/p/7159427.html
Copyright © 2011-2022 走看看