How to check if file exists on Server in C#

By FoxLearn 7/18/2024 7:55:09 AM   3.94K
To check if a file exists on a web server in a C# Windows Forms application, you can use the HttpWebRequest class to send a HEAD request to the URL of the file.

How to check if file exists on Web Server in C#

Creating a simple Windows Forms application allows you to enter a url, then check if file exists on server in c# as shown below.

how to check if file exists in c#

Creating a FileExists method, we will use HttpWebRequest to create a request to web server, then use HttpWebResponse to get respone return from web server.

private bool FileExists(string url)
{
    try
    {
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "HEAD";
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            return (response.StatusCode == HttpStatusCode.OK);
        }
    }
    catch
    {
        return false;
    }
}

If your StatusCode response is OK, we will return true, otherwise false.

Adding a click event handler to the Check button allows you to check if file exists on Server in c#.

private void btnCheck_Click(object sender, EventArgs e)
{
    string url = txtUrl.Text.Trim();

    if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
    {
        MessageBox.Show("Invalid URL format");
        return;
    }

    bool result = FileExists(url);
    if (result)
        MessageBox.Show("The file exists on the web server.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    else
        MessageBox.Show("The file couldn't be found on the web server.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

If the resulting value equals false, your file doesn't exist on the server.

You can also use the HttpClient class to send a HEAD request to the URL of the file.

private async Task<bool> CheckFileExistsAsync(string url)
{
    using (HttpClient client = new HttpClient())
    {
        var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
        return response.IsSuccessStatusCode;
    }
}

The modify the click event handler as shown below.

private async void btnCheck_Click(object sender, EventArgs e)
{
    string url = txtUrl.Text.Trim();

    if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
    {
        MessageBox.Show("Invalid URL format");
        return;
    }

    bool result = await CheckFileExistsAsync(url);
    if (result)
        MessageBox.Show("The file exists on the web server.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    else
        MessageBox.Show("The file couldn't be found on the web server.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

In this example, there's a Windows Forms application with a text box for entering the URL and a button to check if the file exists. When the button is clicked, it checks if the URL is well-formed and then calls the CheckFileExistsAsync method, which sends a HEAD request to the URL. If the response status code is 200 (OK), it means the file exists, otherwise, it doesn't.