0

My application is ASP .NET MVC; I am trying to open multiple files from a directory and store the files names in a variable (collection). For example if I have 10 files named (M1, M2, M3 ...), I am using the following to open one file:

string imagefile =
    System.Web.HttpContext.Current.Server.MapPath("~/Content/" + "M1");

I know the number of files in that directory. Would appreciate your suggestions, thanks in advance.

4

3 回答 3

0

也许是这样:

string[] files = Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath("~/Content/"), "M*");

MSDN:http: //msdn.microsoft.com/en-au/library/wz42302f.aspx

于 2013-02-04T04:18:05.190 回答
0
Public Function GetFilesNames() As List(Of String)

    Dim dir As New System.IO.DirectoryInfo(_filesDirectory)
    Dim lstFiiles As New List(Of String)

    If dir.Exists() Then
        Dim files As FileInfo() = dir.GetFiles("*.png")
        For Each f As FileInfo In files
            lstFiiles.Add(f.Name.Substring(0, f.Name.IndexOf(".")))
        Next
    End If
    Return lstFiiles
End Function
于 2013-02-04T17:43:44.547 回答
0

如果您知道具有正确名称的文件的数量,那么编写代码将非常容易。

使用下面的代码

string[] imagefile = System.Web.HttpContext.Current.Server.MapPath("~/Content/" + "M*");
于 2013-02-04T04:25:05.383 回答