如何从目录Asp.net中读取文件c# [英] How to Read File From Directory Asp.net c#

查看:115
本文介绍了如何从目录Asp.net中读取文件c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我的目录中有很多PDF文件我想在textboc中按文件名检索文件。

我写了这段代码

Hello,
I have many PDF files in my directory i want to retrieve file by file name in textboc.
I have written this code

protected void BindGridview()
    {        
        string[] filesPath = Directory.GetFiles(@"D:\abc\abc\" + textbox1.Text);
        List<listitem> files = new List<listitem>();
        foreach (string path in filesPath)
        {
            files.Add(new ListItem(Path.GetFileName(path)));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
       }
<asp:TextBox ID="txtFormnum" runat="server">
        <asp:Button ID="btnsrch" runat="server" Text="Search" OnClick="btnsrch_Click1" />
        <asp:GridView ID="GridView1" runat="server">
protected void btnsrch_Click1(object sender, EventArgs e)
    {
        BindGridview();
    }



我收到错误目录名无效。





请帮助


I am getting error "The directory name is invalid."


please help

推荐答案

请参阅: http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29。 aspx [ ^ ]。



通常,Web应用程序在沙盒环境中执行,不允许访问文件系统。它仅限于为根网站配置的根目录下的文件(〜/。)。



此外,硬编码时没有情况路径名称很有用。使用它们会导致应用程序的可移植性和维护。在其他一些系统上,你可能不再有某个目录了,那你会怎么做?



-SA


这篇关于如何从目录Asp.net中读取文件c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆