简体   繁体   English

如何将文件夹中所有文件的文件名,大小和上次修改的文件信息获取到gridview中?

[英]How to get filename, size, and last modified fileinfo of all files in folder into gridview?

I know how to look into folders with Directory.GetFiles, and how to use FileInfo for file information, but I can't figure out how to put these concepts together.我知道如何使用 Directory.GetFiles 查看文件夹,以及如何使用 FileInfo 获取文件信息,但我不知道如何将这些概念组合在一起。 When I looked up the Directory class I could only get filenames so I'm stuck, I can only get the gridview to make a 1 dimensional table but I need 3 columns for the info stated in title.当我查找 Directory 类时,我只能获取文件名,所以我被卡住了,我只能获取 gridview 来制作一维表,但我需要 3 列来显示标题中所述的信息。 I'm new to asp.net/c# I'm sorry if the answer is right in my face.我是 asp.net/c# 的新手,如果答案是对的,我很抱歉。 Any help greatly appreciated.非常感谢任何帮助。

//If there are any files in the folder, get info of file

protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "Button Clicked!!!"; protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "Button Clicked!!!";

if (Directory.GetFiles(folderpath).Length > 0)
{
    String name = FileUpload1.FileName;
    //provide file path, the name of file, then grab info of file
    FileInfo file = new FileInfo(folderpath + FileUpload1.FileName);
    //testing if we retrieved info 
    Label1.Text = ("Name :" + file.Name + "<BR/>" + "Size :" + file.Length + "<BR/>" + "Modified :" + file.LastWriteTime + "<BR/>");

    string[] allfiles = Directory.GetFiles(folderpath, "*");
    grdFile.DataSource = allfiles;
    grdFile.DataBind();
}

} }

The code snippet is more me showing my current understanding, not necessarily a proper attempt at what I am trying to do.代码片段更多地展示了我目前的理解,不一定是对我正在尝试做的事情的正确尝试。

This is a odd request, as you want to get Filesystem Information in a Web Server context.这是一个奇怪的请求,因为您想在 Web 服务器上下文中获取文件系统信息。 As they are mostly always on, WebServers tend to be the most interesting targets for hacking.由于它们大部分时间都在运行,因此 Web 服务器往往是最有趣的黑客攻击目标。 In order to compensate for that, they are usually given a seperate user with some of the most restrictive rights possible: Read rights on the Programm and Content Directory.为了弥补这一点,他们通常会被授予一个单独的用户,该用户拥有一些可能的最严格的权限:对程序和内容目录的读取权限。 Maybe sometimes the right to write some subfolder of the Content directory for caching purposes, but even that is not guaranteed.有时可能出于缓存目的而写入 Content 目录的某些子文件夹的权利,但即使如此也不能保证。

As for the actuall code: Currently you are retrieving the File Names/Full File paths.至于实际代码:目前您正在检索文件名/完整文件路径。 That is all the inforamtion that can be stored in a string[] .这就是可以存储在string[]中的所有信息。 You can use them to create the FilInfo instances that will give you the remaining information.您可以使用它们来创建FilInfo 实例这些实例将为您提供剩余信息。

You may have to create a Custom Class, struct or Tupple to hold the data for each file.您可能必须创建自定义类、结构或元组来保存每个文件的数据。 Then create a List of that type.然后创建一个该类型的列表。 Itterate over the list of filenames and annd new instances.遍历文件名列表和新实例。

You can get all the files from a directory and show the properies you want in the GridView.您可以从目录中获取所有文件并在 GridView 中显示您想要的属性。

DirectoryInfo di = new DirectoryInfo(Server.MapPath("/"));
FileInfo[] files = di.GetFiles().OrderBy(p => p.Name).ToArray();

GridView1.DataSource = files;
GridView1.DataBind();

ASPX code ASPX 代码

<asp:GridView ID="GridView1" runat="server" ItemType="System.IO.FileInfo" AutoGenerateColumns="false">
    <Columns>

        <asp:TemplateField HeaderText="FileName">
            <ItemTemplate>
                <%# Item.Name %>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Size">
            <ItemTemplate>
                <%# string.Format("{0:N1}", (decimal)Item.Length / 1024) %> kb
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Modified">
            <ItemTemplate>
                <%# Item.LastWriteTime.ToLongDateString() %>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM