新建文件夹,如果存在就在原有名称后做累加

时间:2019-11-26
本文章向大家介绍新建文件夹,如果存在就在原有名称后做累加,主要包括新建文件夹,如果存在就在原有名称后做累加使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
            var path = @"E:\1";
            var processid = "1";//要创建文件夹的名称
            var VideoPath = string.Empty;
            var filepath = Path.Combine(path, $"{processid}");
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
                VideoPath = filepath;

            }
            else
            {
                int i = 1;
                bool status = false;     
                 var filepath2 = string.Empty;
                while (status != true)
                {
                    var c = processid + $"({i})";
                    filepath2 = Path.Combine(path, $"{c}");
                    if (!Directory.Exists(filepath2))
                    {
                        Directory.CreateDirectory(filepath2);
                        status = true;
                    }
                    else
                    {
                        i++;
                    }
                }
                if (status)
                {
                    VideoPath = filepath2;
                }
            }

原文地址:https://www.cnblogs.com/macT/p/11936432.html