C# 将DirectoryInfo内容加载到文本框中

C# 将DirectoryInfo内容加载到文本框中,c#,asp.net,C#,Asp.net,我在学习本Microsoft教程(步骤2)时遇到问题: 我的根目录中有一个完整的文件列表,因此当我在gridview中单击“编辑”时,文件的内容应该显示在文本框中。 假设我单击edittodefault.aspx,那么应该显示Default.aspx中的内容/代码 .ASPX: <asp:TextBox ID="FileContents" runat="server" Rows="10" TextMode="MultiLine" Width="95%"></asp:Text

我在学习本Microsoft教程(步骤2)时遇到问题:

我的根目录中有一个完整的文件列表,因此当我在gridview中单击“编辑”时,文件的内容应该显示在文本框中。 假设我单击edittodefault.aspx,那么应该显示Default.aspx中的内容/代码

.ASPX:

 <asp:TextBox ID="FileContents" runat="server" Rows="10" TextMode="MultiLine" Width="95%"></asp:TextBox>

    <asp:GridView ID="FilesGrid" DataKeyNames="Name" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:CommandField SelectText="View" ShowSelectButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Length" DataFormatString="{0:N0}" HeaderText="Size in Bytes"
                HtmlEncode="False" />
        </Columns>
    </asp:GridView>
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string appPath = Request.PhysicalApplicationPath;
            DirectoryInfo dirInfo = new DirectoryInfo(appPath);

            FileInfo[] files = dirInfo.GetFiles();

            FilesGrid.DataSource = files;
            FilesGrid.DataBind();
        }
    }

    protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Open the file and display it
        string fullFileName = FilesGrid.SelectedValue.ToString();
        string contents = File.ReadAllText(fullFileName);
        FileContents.Text = contents;
    }

你能改变这个密码吗

    protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Open the file and display it
        string fullFileName = FilesGrid.SelectedValue.ToString();
        string contents = File.ReadAllText(fullFileName);
        FileContents.Text = contents;
    }
这样读

    protected void FilesGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Open the file and display it
        string fullFileName = FilesGrid.SelectedValue.ToString();
        List<string> contents = new List<string>(File.ReadAllLines(fullFileName));
        FileContents.Text = contents.ToString();
    }
protectedvoid FilesGrid\u SelectedIndexChanged(对象发送方,事件参数e)
{
//打开文件并显示它
string fullFileName=FilesGrid.SelectedValue.ToString();
列表内容=新列表(File.ReadAllLines(fullFileName));
FileContents.Text=contents.ToString();
}
*还要确保fullFileName具有完全限定的文件路径+文件名

<asp:GridView ID="FilesGrid" DataKeyNames="Name" runat="server" AutoGenerateColumns="False">

我刚刚插入了您提供的代码,但它没有改变帮助。我不知道您所说的“fullFileName具有完全限定的文件路径+文件名”是什么意思,您能解释一下吗?:)您试图读取的文件。。它是否具有文件路径+文件名,例如“c:\\mypath\\myfilename.txt…”?当您说不起作用时..代码中的哪一行确实失败了..?您是否使用了调试器来逐步调试代码?我还将重构代码以利用一些Try catch{}老实说,我不认识巴迪。我只是遵循了教程,它没有说任何关于这方面的内容。我不确定这是否有帮助,但目前我可以看到我的所有文件,如下图所示:回到我的另一个问题。它实际上是在哪里出错的。你能调试它并报告错误抛出的确切位置吗?Gi给我点时间,伙计,我对这一切都不熟悉,所以我得想办法调试它,这样我才能给你所需要的信息。
<asp:GridView ID="FilesGrid" DataKeyNames="Name" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="FilesGrid_SelectedIndexChanged">