How do I create folders in ASP.NET in code behind?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 06:41:02

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/710123/how-do-i-create-folders-in-asp-net-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!