Last active
September 8, 2020 17:53
-
-
Save corespider/2b91c2f0f2bf3945a8d4ff9ddbfa0cc0 to your computer and use it in GitHub Desktop.
How to Check if file exists on Server in Windows Form C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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