c#中自带函数不会递归创建文件夹,需要自己写函数。
参数为文件路径,如果文件不存在就递归判断其父文件夹是否存在,不存在的话就创建
private void createdir(string filefullpath)
{
bool bexistfile = false;if (File.Exists(filefullpath))
{
bexistfile = true;
}
else //判断路径中的文件夹是否存在
{
string dirpath = filefullpath.Substring(0, filefullpath.LastIndexOf('\\'));
string[] pathes = dirpath.Split('\\');
if (pathes.Length > 1)
{
string path = pathes[0];
for (int i = 1; i < pathes.Length; i++)
{
path += "\\" + pathes[i];
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
}
}
}
本文介绍了一个在C#中实现自动创建文件夹的函数,该函数通过检查文件路径并递归创建不存在的父目录来确保文件夹结构完整。
984

被折叠的 条评论
为什么被折叠?



