如何创建以项目 ID 命名的文件夹
How to create a folder named by item's ID
我想要的是在创建新项目时在 Content/Images
目录中创建一个文件夹。该文件夹应以项目 ID 命名。
示例:
Content/Images/23412/
但由于在数据库分配新 ID 之前 ID 为 0,我想知道我有什么选择?
您可以在将数据插入您的数据库后获取 ID :
myObject.Field1 = "value";
db.SaveChanges()
// You can retrieve the id from the object
int id = myObject.ColumnID;
然后创建您的文件夹:
string path = Server.MapPath("/Content/Images/" + id)
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
我想要的是在创建新项目时在 Content/Images
目录中创建一个文件夹。该文件夹应以项目 ID 命名。
示例:
Content/Images/23412/
但由于在数据库分配新 ID 之前 ID 为 0,我想知道我有什么选择?
您可以在将数据插入您的数据库后获取 ID :
myObject.Field1 = "value";
db.SaveChanges()
// You can retrieve the id from the object
int id = myObject.ColumnID;
然后创建您的文件夹:
string path = Server.MapPath("/Content/Images/" + id)
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}