Skip to content

Instantly share code, notes, and snippets.

@corespider
Last active September 8, 2020 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corespider/2b91c2f0f2bf3945a8d4ff9ddbfa0cc0 to your computer and use it in GitHub Desktop.
Save corespider/2b91c2f0f2bf3945a8d4ff9ddbfa0cc0 to your computer and use it in GitHub Desktop.
How to Check if file exists on Server in Windows Form C#
private void btnCheck_Click(object sender, EventArgs e)
{
bool result = CheckFileExists(txtURL.Text);
if (result)
{
MessageBox.Show("The file exists on the server.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("The file couldn't be found on the server.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private bool CheckFileExists(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(Exception ex)
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment