1

I have a file stored on the server and the web page I am populating depends on the fatc that the file exists or not.

How do I test if the file is available on the server? The file comes on the web page as: http://main.server.com/PGT/Reports/ObjectsReport.xml

I have to test the existence of this file and if it is available I will display a link otherwise I want to hide the link.

The actual path to the server is //main.server.com/inetpub/wwwroot/PGT/Reports/ObjectsReport.xml but I don't have access to the server (and therefore to the file) on the network. I can only access it using the web page. Is there a way to test that the server has the file or not display the link? (hlObjectsReport.Visible = false;)

I have tried to use the following:

Uri validatedUri; Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out validatedUri);

But it returns a valid address even if the file is not there.

Thanks Tony.

Tim
  • 28,212
  • 8
  • 63
  • 76
tony
  • 823
  • 2
  • 16
  • 25

1 Answers1

8

use System.IO.File.Exists() (Documentation)

if(System.IO.File.Exists([path goes here]))
{
  // do something
}

If you're not sure of the physical path, you can substitute the following for [path goes here] above:

Server.MapPath(/PGT/Reports/ObjectsReport.xml)

(Documentation)

David
  • 72,686
  • 18
  • 132
  • 173
  • if you mention Server.MapPath , i would give u +1:) – DarthVader Oct 10 '12 at 19:13
  • :) the force is strong with this one. – DarthVader Oct 10 '12 at 19:29
  • I don't know how to convert /PGT/Reports/ObjectsReport.xml to c:\inetpub/wwwroot/PGT/Reports/ObjectsReport.xml. Server.MapPath(/PGT/Reports/ObjectsReport.xml) returns Failed to map the path '/PGT/Reports/ObjectsReport.xml'. I don't want to hard code the rest of the path because I have other pages and links that are similar to this one. – tony Oct 10 '12 at 19:32
  • 1
    If the path is always absolute, you don't need to use mappath. You can simply specify c:\inetpub\wwwroot\PGT\Reports\ObjectsReport.xml - Wait a minute - this IS on the same server your code is running on, right? If not, that changes things. And if this is a hosted scenario, there are all sorts of possible monkey wrenches that can be thrown in the works. I'd use a different approach, then - it would change my answer completely. IN a hosted environment therye may be multiple servers clustered in ways you're not aware of. – David Oct 10 '12 at 19:35
  • 1
    @tony - If the file is on a DIFFERENT web server, OR you absolutely can't find the PATH, but you know the URL, you can use this answer: http://stackoverflow.com/questions/830435/how-to-check-if-a-file-exists-on-a-server-using-c-sharp-and-the-webclient-class – David Oct 10 '12 at 19:37
  • The file is local to the server that is running the code in the web page. I guess I can do: Uri uri = new Uri(Utils.ComponentsChangedTodayLink); string filename = "c:\\inetpub\\wwwroot" + uri.LocalPath.Replace("/", "\\"); if (!File.Exists(Path.GetFullPath(filename))) { hlProjectsChangedToday.Visible = false; } That works but is that the best way? Thanks – tony Oct 10 '12 at 19:51
  • Yeah, that actually loooks like a way to do it, and if it works, it's probably the best. But it puzzles me why MapPath isn't finding it. It's like the actual path isn't at all what I'd expect it to be. – David Oct 10 '12 at 20:00