||Both the |
Directory classes contain an
Exists() method that accepts as a single parameter the physical path to a file on the Web server's file system and returns a Boolean value indicating whether or not the file exists.
An example of using the
Exists() method is shown below:
If File.Exists("C:\SomeDirectory\SomeFile.txt") then
' file exists...
' file does not exist...
If Directory.Exists("C:\SomeOtherDirectory\") then ...
(Note that in the C# source code the backslash must be escaped by the use of two successive backslashes. However, if the string literal is preceded by an at sign (
@), then escape sequences are not processed, and therefore you do not need to escape the single backslash.)
File class is located in the
System.IO namespace, meaning that you will have to import this namespace into your ASP.NET Web page or code-behind class. If you are using a server-side script block in your ASP.NET Web page, add
<%@ Import Namespace="System.IO" %> to the top of the ASP.NET Web page. If you are using a code-behind class, add the following:
Realize that you can easily translate a virtual path, like
/files/SomeFile.aspx to its corresponding physical path, say
C:\InetPub\wwwroot\files\SomeFile.aspx using the
Server.MapPath() method. For more information on
Server.MapPath() be sure to read Using