如何创建文件夹名称? [英] How to create folder name?

查看:117
本文介绍了如何创建文件夹名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了文件夹,但我想以这种方式命名你。



我尝试过:



我创建文件夹,但我想以这种方式命名。



例如



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









**所以文件夹名称是** StudentInformation



但**我想要StudentInformation1 **因为我想获得1对1 ID < br $>








**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





我想要的名字是** StudentInformation2 **,因为ID是2.如果id是我想要的,我希望id为接下来。



我想创建一个文件夹,我想根据表格中的最后一个id来命名它。我怎么能这样做。



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.

推荐答案

尝试 Path.Combine方法(System.IO)| Microsoft Docs [ ^ ]

Try the Path.Combine Method (System.IO) | Microsoft Docs[^]
string fullPath = Path.Combine(yourPath, folderName);


你可以尝试这个,这是假设表学生在数据库中....您可以根据需要更改DataTable源。



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.

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


好的,我们假设你有一个名为 Studen 的实体和这个实体有一个名为 ID 的属性/属性,不知怎的,你想要在你的实体的文件系统中创建一个文件夹。



解决方案:



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 />





干杯,

AH



Cheers,
AH


这篇关于如何创建文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆