chh
chh

Reputation:

how to create a sub directory inside a directory

In other words, i have temp folder where i store my extracted files. How do i create a folder in that temp folder so that all files are extracted or unzipped in this folder, which is inside the temp folder?

Upvotes: 4

Views: 22438

Answers (6)

Hamed
Hamed

Reputation: 458

System.IO.Directory.CreateDirectory() Creates all directories and subdirectories in the specified path unless they already exist.

string address = Directory.CreateDirectory(newDirectory).CreateSubdirectory("SubDir").FullName;

Upvotes: 0

Rakesh Dadamatti
Rakesh Dadamatti

Reputation: 53

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

Upvotes: 1

Hadi Barak
Hadi Barak

Reputation: 478

Simply Use This :

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

Upvotes: 1

Leonid Shirmanov
Leonid Shirmanov

Reputation: 59

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

Upvotes: 3

Kirtan
Kirtan

Reputation: 21685

Simple

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

Make sure you have the proper rights given to the aspnet worker process to create the folder.

Upvotes: 6

snowcrash09
snowcrash09

Reputation: 4804

System.IO.Directory.CreateDirectory() is what you're looking for.

Upvotes: 1

Related Questions