Directory.getfiles: Security Exception

Discussion in 'ASP.NET 2.0' started by claudiov, Aug 4, 2007.

  1. Hi all, I'm trying to enumerate files in a server directory and I receive a security exception error; I have opened a ticket: here below the thread.
    If you have a solution to successfully enumerate a server directory please let me know ( I just need to fill a string array with directory's filenames).

    Any help is appreciated.
    Best regards.
    Claudio


    Here the ticket history thread:


    Dear Customer,
    </o:p>
    The IUSR / ASPNET users already have change permission on your site.
    </o:p>
    The error you are seeing indicates that your application is trying to retrieve the permission on the file / directory which is not permitted.
    </o:p>
    Frank
    </o:p>
    DiscountASP.NET - Microsoft Gold Certified Partner
    - asp.netPRO Magazine 2007 Readers' Choice Award for Best Hosting Service
    - Visual Studio Magazine 2007 Readers' Choice Award for Best Hosting Service
    </o:p>
    On Fri, 03 Aug 2007 07:00:33 -0700, [email protected] wrote:
    > I'm trying to enumerate the directory images but I'm receiving this error:
    > Server Error in '/SlideShow' Application.
    > --------------------------------------------------------------------------------
    ></o:p>
    > Security Exception
    > Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
    ></o:p>
    > Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
    ></o:p>
    > Source Error:
    ></o:p>
    ></o:p>
    > Line 9: Dim myFileInfo As FileInfo
    > Line 10: Dim i as integer
    > Line 11: FileCollection = Directory.GetFiles("images", "*.png")
    > Line 12: Dim MyFileList(FileCollection.Length) As String
    > Line 13: For i = 0 To FileCollection.Length - 1
    ></o:p>
    ></o:p>
    > Source File: E:\web\mshostingor\htdocs\slideshow\DefaultNew.aspx Line: 11
    ></o:p>
    > It seems to me I need to set the ASPLocal user permission R/W on such directory but I casnnopt do it through the control panel...
    ></o:p>
    > Could you please help me in finding a solution to this issue?...
     
  2. <%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <%@ import Namespace="System.IO" %>
    <script runat="server">
    Sub Page_Load(Sender As Object, E As EventArgs)
    If Not IsPostBack Then
    Dim FileCollection As String()
    Dim myFileInfo As FileInfo
    Dim i as integer
    FileCollection = Directory.GetFiles("images", "*.png")
    Dim MyFileList(FileCollection.Length) As String
    For i = 0 To FileCollection.Length - 1
    myFileInfo = New FileInfo(FileCollection(i))
    MyFileList(i) = myFileInfo.Name.ToString()
    Next
    End If
    End Sub
    </script>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>


    </body>
    </html>
     
  3. I think you need to pass the entire physical path of the directory that you want to search. For example,

    Directory.GetFiles("C:\folder\images", "*.png")

    You can find the physical path of your site's rootin the Account Info page of your hosting control panel.



    Aristotle

    DiscountASP.NET
    www.DiscountASP.NET
     
  4. Bruce

    Bruce DiscountASP.NET Staff

Share This Page