Imports System.IO
Partial Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Note: TestPath is configured the root folder in IIS
Print("-------Path------------")
Print(Server.MapPath("~")) 'Root folder: C:\ALL\Dev\TestPath\
Print(Server.MapPath("~/")) 'Root folder: C:\ALL\Dev\TestPath\
Print(Server.MapPath(".")) 'Current folder: C:\ALL\Dev\TestPath\Folder1\Folder2
Print(Server.MapPath("..")) 'Upper-level folder: C:\ALL\Dev\TestPath\Folder1
Print(Server.MapPath("../..")) 'Upper-upper-level folder: C:\ALL\Dev\TestPath
Print("--------URL-----------")
Print(Request.RawUrl()) '/TestPath/Folder1/Folder2/WebForm1.aspx
Print(Request.PhysicalPath) 'C:\ALL\Dev\TestPath\Folder1\Folder2\WebForm1.aspx
Print(Request.PhysicalApplicationPath) 'C:\ALL\Dev\TestPath\
Print(Request.Url.AbsolutePath) '/TestPath/Folder1/Folder2/WebForm1.aspx
Print(Request.Url.LocalPath) '/TestPath/Folder1/Folder2/WebForm1.aspx
Print("--------File/Folder-----------")
Print(GetCurrentPageName) 'WebForm1.aspx
Print(GetFileExtension(Request.PhysicalPath)) '.aspx
Print(GetFileExtension1(Request.PhysicalPath)) '.aspx
Print(IsFileExist(Request.PhysicalPath)) 'True
Print(IsFolderExist(Server.MapPath(".."))) 'True
Print(IsFolderExist1(Server.MapPath(".."))) 'True
Print(FindFile(Server.MapPath("."), "form1")) 'WebForm1.aspx
End Sub
Private Sub Print(ByVal val As String)
Response.Write(val + "<br>")
End Sub
Public Function GetCurrentPageName() As String
Dim path As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
Dim file As New System.IO.FileInfo(path)
Return file.Name
End Function
Private Function GetFileExtension(ByVal path As String) As String
Dim file As New System.IO.FileInfo(path)
Return file.Extension
End Function
Private Function GetFileExtension1(ByVal path As String) As String
Return System.IO.Path.GetExtension(path)
End Function
Public Shared Function GetFileExtension2(ByVal filename As String) As String
Dim index As Integer = filename.LastIndexOf(".")
Return filename.Substring(index + 1, filename.Length - index - 1)
End Function
Private Function IsFileExist(ByVal path As String) As Boolean
Return File.Exists(path)
End Function
Private Function IsFolderExist(ByVal folder As String) As Boolean
Return Directory.Exists(folder)
End Function
Private Function IsFolderExist1(ByVal folder As String) As Boolean
Return My.Computer.FileSystem.DirectoryExists(folder)
End Function
Private Function FindFile(ByVal path As String, ByVal contains As String) As String
Dim wildcards As String() = {"*.jpg", "*.jpeg", "*.gif", "*.aspx"}
Dim Files As System.Collections.ObjectModel.ReadOnlyCollection(Of String) _
= My.Computer.FileSystem.GetFiles(path, FileIO.SearchOption.SearchTopLevelOnly, wildcards)
For Each f In Files
If f.IndexOf(contains, StringComparison.CurrentCultureIgnoreCase) >= 0 Then
Return System.IO.Path.GetFileName(f)
End If
Next
Return "Not Found"
End Function
End Class