Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I am Creating a directory in C#
C#
if (rows_affected == true)
        {

        
           Directory.CreateDirectory(Server.MapPath(@"~/Documents/Category/")+ Category_Id);
           
        }

This Code Creating a directory with Category_Id

Now I want to Create a SubDirectory , I want to create a directory in Category Id Folder How I do this and Category_Id is not fixed.

C#
if (rows_affected == true)
        {
            Directory.CreateDirectory(Server.MapPath(@"~/Documents/Category") + category_id +  subcategory_id);
          
            Response.Redirect("~/List_SubCategory.aspx");
            Message_Label.ForeColor = System.Drawing.Color.Green;
        }
Posted
Updated 4-May-15 20:25pm
v3
Comments
[no name] 5-May-15 2:08am    
What is your problem?
Gurpreet Arora Malhotra 5-May-15 2:09am    
How I define that Creating a sub directory in this category id
KaushalJB 5-May-15 2:09am    
Ok so you having issues ?

1 solution

Basically, you need a "\" between the "Category_Id" and the "subcategory_id"
At the moment, (assuming your Cat ID is "ABC" and your sub cat is "DEF") you are asking teh system to create
D:\Temp\ABC
And then
D:\Temp\ABCDEF
When what you want is
D:\Temp\ABC\DEF

The best way to do this is to use Path.Combine:
C#
string path = Path.Combine(Server.MapPath(@"~/Documents/Category/"), category_id,  subcategory_id);
Directory.CreateDirectory(path)
As this will add and remove slashes as necessary.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900