如何创建于code文件夹中的ASP.NET的背后?文件、创建于、夹中、NET

2023-09-04 06:57:43 作者:回忆狠美却狠伤人

我想在运行时创建动态文件夹。通过一个文本框和输出输入文件夹名称将显示在一个TreeView。

I want to create dynamic folders at run time. Folder names with be input via a TextBox and output will be displayed in a TreeView.

的形式将提交如果我进入第一个文件夹的名称为TextBox1的,然后点击添加文件夹按钮。当我提交多个文件夹同名的输出应该是名称的索引增量。例如。 FooFolder,FooFolder(2),FooFolder(3),等等。

The form will submit if I enter the first folder name into textbox1 and click the "Add Folder" button. When I submit multiple folders with the same name the output should be an indexed increment of the name. Eg. FooFolder, FooFolder(2), FooFolder(3), etc.

如果我删除FooFolder(2),然后重新创建一个名为FooFolder一个文件夹,该文件夹应该是FooFolder(2),如果我再创建一个文件夹,那么它应该是FooFolder(4)。

If I delete FooFolder(2) and then recreate a folder with the name FooFolder, the folder should be FooFolder(2), and if I create one more folder then it should be FooFolder(4).

有关删除的人可以选择将显示在TextBox2中TreeView中的特定文件夹,然后单击删除文件夹按钮。

For deletion one can select the particular folder from the TreeView which will be displayed in TextBox2 and click the "Remove Folder" button.

下面是我的presentation code:

Here is my presentation code:

<asp:Button ID="btnAddFolder" runat="server" Height="24px" Text="Add Folder" 
        Width="148px" onclick="btnAddFolder_Click" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnRemoveFolder" runat="server" Text="Remove Folder" />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" 
        NodeIndent="15">
        <ParentNodeStyle Font-Bold="False" />
        <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
        <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" 
            HorizontalPadding="0px" VerticalPadding="0px" />
        <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" 
            HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
    </asp:TreeView>

现在我怎么写code做的工作?

Now how do I write the code to do the work?

推荐答案

你的问题有点不清楚,但我会尽力让你一点办法也无妨。

Your question is a bit unclear, but I'll try to get you a little ways there anyway.

首先要确保您导入/使用(根据语言)System.IO命名空间这个工作。但是你可以做的就是这样的事情。

First of all be sure that you are importing/using (depending on language) the System.IO namespace for this to work. But what you can do is something like this.

string pathToCreate = "~/UserFolders/" + TextBox1.Text;
if(Directory.Exists(Server.MapPath(pathToCreate))
{
   //In here, start looping and modify the path to create to add a number
   //until you get the value needed
}

//Now you know it is ok, create it
Directory.CreateDirectory(Server.MapPath(pathToCreate));

这应该帮助你无论如何要与该文件夹的创建。

This should help get you going with the folder creation anyway.