IT Blog

  • Blog
  • Technology
    • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • 中文中文
    • ENEN
Experience IT
In a World of Technology, People Make the Difference.
  1. Home
  2. Technology
  3. Program Language
  4. C#
  5. Checks if the remote file exists

Checks if the remote file exists

2020-11-19 820 Views 1 Like 0 Comments

Language: C#

/// <summary>Get remote file information</summary>
/// <param name="url">The URL of the remote file</param>
/// <returns>RemoteFile if the remote file exists, or null</returns>
public static RemoteFile RemoteFileInfo(string url) {
    RemoteFile file = new RemoteFile();
    try {
        var request = WebRequest.Create(url);
        request.Method = "HEAD";
        using (var response = request.GetResponse() as HttpWebResponse) {
            file.LastModified = response.LastModified;
            file.Size = response.ContentLength;
            file.StatusCode = response.StatusCode;
            file.ContentType = response.ContentType;
            file.ContentEncoding = response.ContentEncoding;
        }
    }
    catch(Exception e) {
        return null;
    }
    return file;
}

public class RemoteFile {
    public DateTime LastModified { get; set; }
    public long Size { get; set; }
    public HttpStatusCode StatusCode { get; set; }
    public string ContentType { get; set; }
    public string ContentEncoding { get; set; }
}

 3,185 total views,  9 views today

error
fb-share-icon
Tweet
fb-share-icon
IT Team
Author: IT Team

Tags: None
Last updated:2020-11-19

IT Team

This person is lazy and left nothing

Like
Next >

Comments

Cancel reply
Chinese (Simplified) Chinese (Simplified) Chinese (Traditional) Chinese (Traditional) English English French French German German Japanese Japanese Korean Korean Russian Russian
Newest Hotspots Random
Newest Hotspots Random
Rich editor not working Making web page scroll down automatically Getting data from Dapper result All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions
How to keep and display contact form 7 data Batch rename multiple files on Windows 10 Add slideshow on homepage for Parallax One theme WordPress database access with $wpdb Tracking referral ID for registered user in WordPress Geting twitter streaming contents in WordPress
Categories
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 2021 Hostlike IT Blog. All rights reserved.

This site is supported by Hostlike.com