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

时间:2022-12-04 08:06:44

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.

我想在运行时创建动态文件夹。通过TextBox和输出输入的文件夹名称将显示在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.

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

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

如果我删除FooFolder(2)然后重新创建一个名为FooFolder的文件夹,该文件夹应该是FooFolder(2),如果我再创建一个文件夹,那么它应该是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.

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

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?

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

2 个解决方案

#1


17  

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.

首先要确保您正在导入/使用(取决于语言)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));

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

这应该有助于您进行文件夹创建。

#2


-1  

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

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

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.

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

#1


17  

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.

首先要确保您正在导入/使用(取决于语言)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));

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

这应该有助于您进行文件夹创建。

#2


-1  

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

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

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.

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