0

I'm trying to use System.Web; to read and write into text document located on server from winform desktop application. First of all need to check if file exist. Not sure why, but System.Web does not works it is added for References as System.Web

string siteDir = "http://www.site.info/doc.txt";

if (File.Exists(Server.MapPath(siteDir)));

same with System.Web.HttpContext

System.Web.HttpContext.Current.Server.MapPath(siteDir);
nikorio
  • 671
  • 4
  • 16
  • 28

2 Answers2

1

A simple way to do this is to use WebClient.DownloadFile() to attempt to download a file from a url like the one you are using. If a WebException is thrown and the HTTP Error is 404 File Not Found, then the file does not exist on the server (or you do not have access to it).

See: How to catch 404 WebException for WebClient.DownloadFileAsync for info on how to detect a 404 error.

Unfortunately there is no WebClient method that will simply check to see if a file is present on the server.

Community
  • 1
  • 1
Keith Payne
  • 3,002
  • 16
  • 30
  • Hello, thanks, so I did it this way HttpWebResponse resp = ex.Response as HttpWebResponse; if (resp.StatusCode == HttpStatusCode.NotFound) { //404 } – nikorio Mar 14 '17 at 04:17
-1
use System.IO.File.Exists() 

if(System.IO.File.Exists([path of file]))
{
  // do something
}
AKN
  • 416
  • 2
  • 5