如何在代码隐藏页面中创建文件夹

发布于 2024-07-16 03:32:18 字数 1854 浏览 2 评论 0原文

这是参考昨天的问题“How do我在 ASP.NET 中的代码隐藏中创建文件夹”。 问题是我想在运行时创建动态文件夹。 文件夹名称将通过文本框输入,输出将显示在 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);
}

在我的代码中,ChildNode 索引不会递增; 它始终为 1,如下所示:

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

在树视图中,我设置了 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)

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

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

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:

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:

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:

-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).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や三分注定 2024-07-23 03:32:18

这里的问题是检测该项目是否存在的算法。 基本上你的代码:

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;            
}

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

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

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

您比较如果 TreeView1.Nodes[i].Text == TextBox1.Text,则只有一个节点具有此名称。 您需要去掉名称的数字部分。

如果您使用像 NewFolder(1) 这样的命名约定,那么您可以轻松地做到这一点。 但根据您那里的代码,节点的名称将是 NewFolder1

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.

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

Now the user clicks add again for NewFolder, this time it finds NewFolder so the new name becomes 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.

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

池予 2024-07-23 03:32:18

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

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)

行雁书 2024-07-23 03:32:18

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

它应该看起来像:

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

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++
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文