关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

c#递归创建文件夹

发布时间:2019-12-23 18:42:17

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

                        }

                    }

                }

            }

}



/template/Home/Zkeys/PC/Static