如何在代码隐藏的 ASP.NET 中创建文件夹?

发布于 2024-07-16 10:18:37 字数 1414 浏览 2 评论 0原文

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

如果我在 textbox1 中输入第一个文件夹名称并单击“添加文件夹”按钮,该表单将提交。 当我提交具有相同名称的多个文件夹时,输出应该是名称的索引增量。 例如。 FooFolder、FooFolder(2)、FooFolder(3) 等。

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

要删除,可以从 TreeView 中选择将显示在 TextBox2 中的特定文件夹,然后单击“删除文件夹”按钮。

这是我的演示代码:

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

现在我该如何编写代码来完成这项工作?

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.

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.

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

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

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>

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

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

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

发布评论

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

评论(2

§普罗旺斯的薰衣草 2024-07-23 10:18:37

你的问题有点不清楚,但无论如何我会尽力帮助你解决这个问题。

首先,请确保您正在导入/使用(取决于语言)System.IO 命名空间,以使其正常工作。 但你能做的就是这样的。

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

无论如何,这应该可以帮助您继续创建文件夹。

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

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.

回眸一笑 2024-07-23 10:18:37

您可以读取子目录的目录,将目录名称与给定名称(在文本框中输入)进行比较。 如果找到,您可以附加计数器值。 如果文件夹名称类似于“()”,则需要增加此计数器。
获得正确的名称后,即附加计数器值后,您可以调用 win32 API 来创建目录

int SHCreateDirectory(HWND hwnd, LPCWSTR pszPath);

编辑:您可以根据操作系统、技术调用特定的 api 来创建目录。 以上适用于 Win32。

You can read the directory for the sub directories, compare the directory names with the given name(entered in textbox). If it is found you can append the counter value. You need to increment this counter if the folder name is like "()".
After getting the proper name i.e., after appending the counter value you can call win32 API to create the directory

int SHCreateDirectory(HWND hwnd, LPCWSTR pszPath);

EDIT: You can call the specific apis depending on the OS, technology to create the directory. Above is for Win32.

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