如何在页面后面的代码中创建文件夹

时间:2022-12-04 08:11:10

This is in reference to yesterday's question "How do I create folders in ASP.NET in code behind". The problem is that I want to create dynamic folders at run time. Folder names will be entered via a TextBox and output will be displayed in a TreeView. 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 (e.g., FooFolder, FooFolder(2), FooFolder(3), etc). There are two events: Add Folder Event and Remove Folder Event. If I select a particular child folder and click on the "Remove folder" button, the folder will be removed. For adding a folder I have written the following code:

这是参考昨天的问题“如何在代码隐藏的ASP.NET中创建文件夹”。问题是我想在运行时创建动态文件夹。文件夹名称将通过TextBox输入,输出将显示在TreeView中。如果我在textbox1中输入第一个文件夹名称并单击“添加文件夹”按钮,表单将提交。当我提交具有相同名称的多个文件夹时,输出应该是名称的索引增量(例如,FooFolder,FooFolder(2),FooFolder(3)等)。有两个事件:添加文件夹事件和删除文件夹事件。如果我选择一个特定的子文件夹并单击“删除文件夹”按钮,该文件夹将被删除。为了添加文件夹,我编写了以下代码:

TreeNode tnode = new TreeNode();
if (TreeView1.Nodes.Count > 0)
        {
            int found = 0;
            for (int i = 0; i < TreeView1.Nodes.Count; i++)
            {
                if (TreeView1.Nodes[i].Text == TextBox1.Text)
                    found += 1+i;
            }
            if (found > 0)
            {
                tnode.Text = TextBox1.Text + found.ToString();
            }
            else
            {
                tnode.Text = TextBox1.Text;
            }
        }
        else
        {
            tnode.Text = TextBox1.Text;
        }
        TreeView1.Nodes.Add(tnode);
}

In my code, the ChildNode index is not incrementing; it is always 1, like this:

在我的代码中,ChildNode索引没有递增;它总是1,像这样:

Sumit
Sumit(1)
Sumit(1)
Sumit(1)
Amit
Amit(5)
Amit(5)
Amit(5)

In the treeview, I have set ImageSet="XPFileExplorer". So the output should look like this:

在树视图中,我设置了ImageSet =“XPFileExplorer”。所以输出应该如下所示:

-Root
        -Sumit(Parent1)
                  NewFolder
                  NewFolder(2)
                  NewFolder(3)
                  NewFolder(4)
                  NewFolder(5)
         -Amit(Parent2)
                  FooFolder
                  FooFolder(2)
                  FooFolder(3)
                  FooFolder(4)
                  FooFolder(5)

If I delete any child folder, say, Newfolder(3) and Newfolder(4) and create these same folders under the same Sumit(Parent1), the index should be Newfolder(3),Newfolder(4). If I create one more NewFolder under Sumit with same name then the index should be NewFolder(6).

如果我删除任何子文件夹,比如Newfolder(3)和Newfolder(4)并在相同的Sumit(Parent1)下创建这些相同的文件夹,则索引应为Newfolder(3),Newfolder(4)。如果我在Sumit下使用相同的名称再创建一个NewFolder,那么索引应该是NewFolder(6)。

Could somebody please modify my code to get this desired output?

有人可以修改我的代码以获得所需的输出吗?

3 个解决方案

#1


Your issue here is your algorithim to detect if the item exists. Basically your code:

您的问题是您的算法,以检测项目是否存在。基本上你的代码:

for (int i = 0; i < TreeView1.Nodes.Count; i++)            
{                
    if (TreeView1.Nodes[i].Text == TextBox1.Text)
                    found += 1+i;            
}
if (found > 0)            
{                
   tnode.Text = TextBox1.Text + found.ToString();      
}            
else            
{                
   tnode.Text = TextBox1.Text;            
}

Let's walk through this. The user submits NewFolder your code goes through and doesn't find any node called NewFolder, so it sets the node to NewFolder.

让我们来看看这个。用户提交您的代码经过的NewFolder并且找不到任何名为NewFolder的节点,因此它将节点设置为NewFolder。

Now the user clicks add again for NewFolder, this time it finds NewFolder so the new name becomes NewFolder1.

现在用户再次为NewFolder点击添加,这次它找到NewFolder,因此新名称变为NewFolder1。

Now the user clicks add again for NewFolder, this time it finds NewFolder so the new name becomes NewFolder1.

现在用户再次为NewFolder点击添加,这次它找到NewFolder,因此新名称变为NewFolder1。

Your comparing if TreeView1.Nodes[i].Text == TextBox1.Text, which only one node will ever have this name. You will need to strip off the numeric portion of the name.

你的比较是TreeView1.Nodes [i] .Text == TextBox1.Text,只有一个节点会有这个名字。您将需要剥离名称的数字部分。

If your using a naming convention like NewFolder(1) then you can easily do this. But based on the code you have there the name of the node would be NewFolder1

如果您使用像NewFolder(1)这样的命名约定,那么您可以轻松地执行此操作。但根据你所拥有的代码,节点的名称将是NewFolder1

#2


Before you do this, I learned the hard way that you should not create/remove folders under a running application, or you will cause your app pool to recycle. So make sure that you are creating directories somewhere else on the server. (Hopefully you have that access)

在您执行此操作之前,我了解到您不应该在正在运行的应用程序下创建/删除文件夹,或者您将导致应用程序池回收。因此,请确保您在服务器上的其他位置创建目录。 (希望你有这个访问权限)

#3


Your text comparison is off. Since you may have added numbers to previous nodes under the same parent, you will only encounter the new name once.

您的文字比较已关闭。由于您可能已将数字添加到同一父节点下的先前节点,因此您只会遇到一次新名称。

It should look like:

它应该看起来像:

if (TreeView1.Nodes[i].Text.StartsWith(TextBox1.Text))
    found++

#1


Your issue here is your algorithim to detect if the item exists. Basically your code:

您的问题是您的算法,以检测项目是否存在。基本上你的代码:

for (int i = 0; i < TreeView1.Nodes.Count; i++)            
{                
    if (TreeView1.Nodes[i].Text == TextBox1.Text)
                    found += 1+i;            
}
if (found > 0)            
{                
   tnode.Text = TextBox1.Text + found.ToString();      
}            
else            
{                
   tnode.Text = TextBox1.Text;            
}

Let's walk through this. The user submits NewFolder your code goes through and doesn't find any node called NewFolder, so it sets the node to NewFolder.

让我们来看看这个。用户提交您的代码经过的NewFolder并且找不到任何名为NewFolder的节点,因此它将节点设置为NewFolder。

Now the user clicks add again for NewFolder, this time it finds NewFolder so the new name becomes NewFolder1.

现在用户再次为NewFolder点击添加,这次它找到NewFolder,因此新名称变为NewFolder1。

Now the user clicks add again for NewFolder, this time it finds NewFolder so the new name becomes NewFolder1.

现在用户再次为NewFolder点击添加,这次它找到NewFolder,因此新名称变为NewFolder1。

Your comparing if TreeView1.Nodes[i].Text == TextBox1.Text, which only one node will ever have this name. You will need to strip off the numeric portion of the name.

你的比较是TreeView1.Nodes [i] .Text == TextBox1.Text,只有一个节点会有这个名字。您将需要剥离名称的数字部分。

If your using a naming convention like NewFolder(1) then you can easily do this. But based on the code you have there the name of the node would be NewFolder1

如果您使用像NewFolder(1)这样的命名约定,那么您可以轻松地执行此操作。但根据你所拥有的代码,节点的名称将是NewFolder1

#2


Before you do this, I learned the hard way that you should not create/remove folders under a running application, or you will cause your app pool to recycle. So make sure that you are creating directories somewhere else on the server. (Hopefully you have that access)

在您执行此操作之前,我了解到您不应该在正在运行的应用程序下创建/删除文件夹,或者您将导致应用程序池回收。因此,请确保您在服务器上的其他位置创建目录。 (希望你有这个访问权限)

#3


Your text comparison is off. Since you may have added numbers to previous nodes under the same parent, you will only encounter the new name once.

您的文字比较已关闭。由于您可能已将数字添加到同一父节点下的先前节点,因此您只会遇到一次新名称。

It should look like:

它应该看起来像:

if (TreeView1.Nodes[i].Text.StartsWith(TextBox1.Text))
    found++