如何在目录中创建子目录

时间:2009-04-21 08:14:11

标签: c# directory

换句话说,我有临时文件夹,我存储我提取的文件。如何在该临时文件夹中创建一个文件夹,以便在此文件夹中提取或解压缩所有文件,该文件夹位于临时文件夹中?

5 个答案:

答案 0 :(得分:4)

简单

Directory.CreateDirectory(Path.Combine("<Your temp folder>", "<DirectoryName>"));

确保您拥有对aspnet工作进程的适当权限以创建文件夹。

答案 1 :(得分:3)

string tempFolderAbsolutePath = @"C:\Temp";
string subFolderRelativePath = @"SubTemp1";

DirectoryInfo tempFolder = new DirectoryInfo( tempFolderAbsolutePath );
DirectoryInfo subFolder = tempFolder.CreateSubdirectory( subFolderRelativePath );

string tempFileName = String.Concat( Guid.NewGuid().ToString(), @".tmp" );
string textData = @"Temp text data";

using (StreamWriter streamWriter = File.CreateText( Path.Combine( subFolder.FullName, tempFileName ) ))
{
        streamWriter.Write( textData );
        streamWriter.Flush();
        streamWriter.Close();
}

答案 2 :(得分:1)

答案 3 :(得分:1)

只需使用:

System.IO.Directory.CreateDirectory(String.Format(@"{0}/{1}", PathToParent, SubDirectoryName)

答案 4 :(得分:0)

FRONT END

        <form id="form1" runat="server">
            <asp:Button ID="Button1" runat="server" Text="Create New Directory" onclick="createButton_Click" />            
            <br /><br />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        </form>

CODEBEHIND

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;

namespace RakeshDadamatti
{
    public partial class CreateDirectory : System.Web.UI.Page
    {
        String newDirectory;
        String subDirectory;
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        private void CreatenewDirectory(string newDirectory)
        {
            try
            {
                if (!Directory.Exists(newDirectory))
                {
                    Directory.CreateDirectory(newDirectory);
                    Label1.Text = "Directory Has Been Created.";
                }
                else
                {
                    Label1.Text = "Directory Exists.";
                }

                if (!Directory.Exists(subDirectory))
                {
                    Directory.CreateDirectory(subDirectory);
                    Label2.Text = "Sub Directory Has Been Created.";
                }
                else
                {
                    Label2.Text = "Sub Directory Exists.";
                }
            }
            catch (IOException _err)
            {
                Response.Write(_err.Message);
            }
        }
        protected void createButton_Click(object sender, EventArgs e)
        {
            newDirectory = Server.MapPath("Directory Name Here");
            subDirectory = Server.MapPath(@"" + "~/" + newDirectory + "/" + "Sub Directory Name Here");
            CreatenewDirectory(newDirectory);
        }
    }
}