Click here to Skip to main content
15,887,393 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I create folder but I'd like to name you this way.

What I have tried:

I create folder but I'd like to name you this way.

For example

Table Student

    ID   NAME  SURNAME
    1    Alex   John


Code is:

string folder="StudentInformation";
    
    Directory.CreateDirectory(Path + folder);




**so folder name is** StudentInformation

but **I want to StudentInformation1** because I want to get 1 for 1 ID


ID   NAME  SURNAME
     1    Alex   John
    
     2    Marry  Emily


I want to name is **StudentInformation2** because ID is 2.If id is what I want, I want id to be next.

I want to create a folder and I want to name it according to the last id value in the table.How can I do it.
Posted
Updated 8-Nov-18 5:34am
v3

Try the Path.Combine Method (System.IO) | Microsoft Docs[^]
C#
string fullPath = Path.Combine(yourPath, folderName);
 
Share this answer
 
Comments
[no name] 7-Nov-18 14:26pm    
I write path+folder. Created folder and folder name result is => InformationStudent

But I want to name is InformationStudent + ( the last id value is if you want it.)

I couldn't make this area ( the last id value is if you want it.)

Last ID :2 Name is => InformationStudent2
Last ID: 3 Name is => InformationStudent3 etc.
You can try this out, which was written under the assumption that Table Student is in a database.... You can alter the DataTable source as you need.

C#
DataTable dt = new DataTable();

using (SqlConnection conn = new SqlConnection(YourConnectionString)) {
	SqlCommand cmd = new SqlCommand("SELECT [ID] FROM [Student]", conn);
	conn.Open()
	SqlDataReader dr = cmd.ExecuteReader();
	if (dr.HasRows) { dt.Load(dr); }
	conn.Close();
}

foreach(DataRow row in dt.Rows) {
	string folder="StudentInformation" + row["ID"].ToString();
	Directory.CreateDirectory(Path + folder);
}
 
Share this answer
 
Comments
[no name] 7-Nov-18 16:24pm    
Hi.I need to use entity.How do I get your connection string.Can I do it without using the SQL connection string?
MadMyche 7-Nov-18 17:09pm    
If you are using EF, then you should just be able to use LINQ to populate
foreach (Student stud in StudentCollection) { string folder="StudentInformation" + stud.id.ToString(); Directory.CreateDirectory(Path + folder); }
Ok, let's assume that you have an entity called Studen and this entity has an attribute/property called ID and somehow you reach a point that you want to create a folder in file-system for your entity.

Solution:

//Model is your entity context which contains all students.<br />
Student student = Model.Students.FirstOrDefault(); <br />
string folder="StudentInformation";  <br />
Directory.CreateDirectory(Path + folder + sudent.ID.ToString());<br />


Cheers,
AH
 
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