0

我正在使用以下代码尝试写出服务器根目录下的所有文件...

这是我到目前为止的代码......

<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server" explicit="true" strict="true">
Dim position As Integer

Public Sub GetFiles(ByVal path As String)
    If File.Exists(path) Then
        ' This path is a file 
        ProcessFile(path)
    ElseIf Directory.Exists(path) Then
        ' This path is a directory 
        ProcessDirectory(path)
    End If
End Sub

' Process all files in the directory passed in, recurse on any directories 
' that are found, and process the files they contain. 
Public Sub ProcessDirectory(ByVal targetDirectory As String)
    ' Process the list of files found in the directory. 
    Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
    For Each fileName As String In fileEntries
        ProcessFile(fileName)
    Next

    ' Recurse into subdirectories of this directory. 
    Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
    For Each subdirectory As String In subdirectoryEntries
        ProcessDirectory(subdirectory)
    Next
End Sub

' Insert logic for processing found files here. 
Public Sub ProcessFile(ByVal path As String)
    Dim fi As New FileInfo(path)
    Response.Write("File Number " + position.ToString() + ". Path: " + path + " <br />")
    position += 1
End Sub

GetFiles("\images\")

</script>

我在以下代码行中收到声明预期编译错误:

GetFiles("\images\")

我有什么需要在这里声明的吗?我只是扯掉我的头发并在这个上秃顶...... arggg!

4

1 回答 1

1

内联脚本(意味着 .aspx 标记的一部分,而不是后面的代码)只能包含方法,而不是命令。

虽然文档中没有明确提到,但命名代码声明块暗示它仅用于声明代码。您在其他地方或事件中调用该代码。

因此,您必须将要执行的所有命令放在页面事件中,在您的情况下 Page_Load 看起来最合适:

Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
    GetFiles("\images\")
End Sub

如果您希望它成为标记本身的一部分,那么您可以使用<% ... %>符号而不是将其放入<script>将失败的标签中:

<!-- markup here -->
<!-- .... -->
<% GetFiles("\images\") %>
于 2013-06-02T06:47:24.957 回答