public class GetUrl
{
public static string GetAbsoluteUrl(string strRelativePath)
{
if (string.IsNullOrEmpty(strRelativePath))
return strRelativePath;
string strFullUrl;
if (strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase)
|| strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase)
|| strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase)
)
{
// 已經是絕對路徑。
strFullUrl = strRelativePath;
}
else
{
// 是相對路徑,必須轉換成絕對路徑。
strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri;
if (strFullUrl.IndexOf("ClientBin") > 0)
strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + strRelativePath;
else
strFullUrl = strFullUrl.Substring(0, strFullUrl.LastIndexOf("/") + 1) + strRelativePath;
}
return strFullUrl;
}
}
===============================================
Public Class GetUrl
Public Shared Function GetAbsoluteUrl(ByVal strRelativePath As String) As String
Dim strFullUrl As String
If String.IsNullOrEmpty(strRelativePath) Then
Return strRelativePath
End If
If strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase) _
Or strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase) _
Or strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase) Then
strFullUrl = strRelativePath
Else
strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri
If (strFullUrl.IndexOf("ClientBin") > 0) Then
strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + strRelativePath
Else
strFullUrl = strFullUrl.Substring(0, strFullUrl.LastIndexOf("/") + 1) + strRelativePath
End If
End If
Return strFullUrl
End Function
End Class
沒有留言:
張貼留言