Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Hello,
I have many PDF files in my directory i want to retrieve file by file name in textboc.
I have written this code
C#
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
Posted
Updated 25-Sep-14 3:47am
v2
Comments
joshrduncan2012 25-Sep-14 8:56am    
So what's your question?
[no name] 25-Sep-14 8:57am    
And does your server have a directory named "D:\abc\abc" on it? Does your account running your IIS service have access to that directory?

1 solution

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

Normally, Web applications are executed in a sandboxed environment which does not allow the access to the file system. It is limited to the files under the root directory of configured for a give site ("~/.").

Moreover, there a no situations when hard-code paths names are useful. Using them kills portability and maintenance of the application. On some other system, you may not have some directory anymore, so what would you do?

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900