2012-10-15 116 views
0

當我嘗試使用asp.net查看源代碼時,出現此錯誤。顯示代碼錯誤

訪問路徑'C:\ Users \ user \ Desktop \ Website1'被拒絕。

任何想法如何解決這個問題?

顯示代碼如下。我不知道如何解決這個問題。

<%@ Page Language="C#" runat="server" Debug="true" %> 
    <%@ Import Namespace="System.IO" %> 
    <script language="C#" runat="server"> 
void Page_Load() 
{ 
string filePath = 
Server.MapPath(Request.QueryString["filename"]); 
FileName.Text = Request.QueryString["filename"]; 
FileInfo file = new FileInfo(filePath); 

if (file.Extension != ".mdb" 
&& file.Extension != ".xml" 
&& file.Extension != ".exe") 
{ 
Code.Text = ReadFile(filePath); 
} 
    else 
{ 
    Code.Text = "Sorry you can't read a file with an extension of " + file.Extension; 
} 
    } 

    private string ReadFile(string filepath) 
    { 
     string fileOutput = ""; 
    try 
    { 
     StreamReader FileReader = new StreamReader(filepath); 
     //The returned value is -1 if no more characters are 
      //currently available. 
     while (FileReader.Peek() > -1) 
     { 
     //ReadLine() Reads a line of characters from the 
      //current stream and returns the data as a string. 
     fileOutput += FileReader.ReadLine().Replace("<", "&lt;"). 
     Replace(" ", "&nbsp;&nbsp;&nbsp;&nbsp;") 
        + "<br />"; 
      } 
      FileReader.Close(); 
     } 
      catch (FileNotFoundException e) 
      { 
     fileOutput = e.Message; 
     } 
     return fileOutput; 
     } 
    </script> 

     <html> 
     <head> 
    <title>code</title> 
     <link rel="stylesheet" href="style1.css" type="text/css"> 
     </head> 
     <body> 
      <h1 class="pageHeader">Source Code</h1> 
      <asp:label id="FileName" 
       CssClass="codeheader" Runat="server"/> 
     <asp:Panel id="pnlCode" CssClass="code" 
     runat="server" Width="80%"> 
      <asp:label id="Code" Runat="server" /> 
    </asp:Panel> 
    </body> 
     </html> 
+0

*此*文件放置在您的web應用程序下?如果*否*那麼你無法閱讀它。 – adatapost

回答

0

右擊你的Website1,點擊屬性,轉到security,點擊edit,然後添加network service,看看它是否解決您的問題。

+0

如果沒有幫助,請嘗試訪問所有人並查看它是否有效 –

+0

仍然無法正常工作。我可以訪問網絡服務和所有人。我的代碼中的錯誤是這一行。 StreamReader FileReader =新的StreamReader(文件路徑); – soe

+0

道路是否存在於現實中? –