2011-12-23 120 views
3

我有一個相當簡單的任務,從ASP.Net Web應用程序(虛擬文件夾位於網站的根目錄)之外的虛擬文件夾中檢索圖像文件,然後將其顯示在一個圖像控件。我能夠訪問網站內的虛擬文件夾。這是我的代碼從虛擬文件夾檢索文件

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    Dim sourcefile As String = Server.MapPath("~/Common/Images/LAPDPatch.jpg") 
    Image1.ImageUrl = sourcefile 
End Sub 

我在做什麼錯?

好吧,我能弄明白:

解決方案

好吧,我能得到它的工作,我用錯了「斜線」角色,我用的是正斜槓,而不是反斜槓:

Dim sourcefile As String = Server.MapPath("\VitualFolderName\") & "Images\LAPDPatch.jpg" 

感謝所有回覆!

回答

3

使用ResolveUrl

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    'root of currently running app, virtual or not 
    Dim sourcefile As String = Page.ResolveUrl("~/Common/Images/LAPDPatch.jpg") 

    'root of site 
    'Dim sourcefile As String = "/Common/Images/LAPDPatch.jpg" 

    'root of another site 
    'Dim sourcefile As String = "/VirtualDir/Common/Images/LAPDPatch.jpg" 

    Image1.ImageUrl = sourcefile 
End Sub 
+0

謝謝,這是你的代碼生成的網址「C:\ inetpub \ wwwroot \ MyWebSite \ VirtualFolderName \ Images \ LAPDPatch.jpg」 – 2011-12-23 01:35:19

+0

不知道你一開始想做什麼,看更新。 – 2011-12-23 01:54:55

+0

Rick再次感謝,如果虛擬文件夾是網站中的子文件夾,上面的代碼工作正常。我想要做的是訪問位於IIS服務器根目錄下的虛擬文件夾。 – 2011-12-23 01:58:41

0

確保Web應用程序正在運行,並且用戶(也許「IIS應用程序池\默認的應用程序池」下IIS7)訪問該文件夾,它可能只是一個NTFS權限的東西。

+0

謝謝,我正在使用錯誤的斜槓查看我原來的帖子。 – 2011-12-23 16:30:51