c#递归创建文件夹

来源:互联网 发布:lake软件 编辑:程序博客网 时间:2024/05/17 01:14

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

}