如果要创建的文件夹已经存在,我正在尝试处理。要在文件夹名称中添加数字,请执行以下操作。就像windows资源管理器..例如(新建文件夹、新建文件夹1、新建文件夹2 ..)我怎么才能递归呢?我知道这段代码是错误的。我如何修复或者修改下面的代码来解决这个问题?
int i = 0;
private void NewFolder(string path)
{
string name = "\\New Folder";
if (Directory.Exists(path + name))
{
i++;
NewFolder(path + name +" "+ i);
}
Directory.CreateDirectory(path + name);
}发布于 2012-01-30 01:09:39
为此,您不需要递归,而是应该寻找迭代解决方案
private void NewFolder(string path) {
string name = @"\New Folder";
string current = name;
int i = 0;
while (Directory.Exists(Path.Combine(path, current)) {
i++;
current = String.Format("{0} {1}", name, i);
}
Directory.CreateDirectory(Path.Combine(path, current));
}发布于 2012-01-31 22:11:25
private void NewFolder(string path)
{
string name = @"\New Folder";
string current = name;
int i = 0;
while (Directory.Exists(path + current))
{
i++;
current = String.Format("{0} {1}", name, i);
}
Directory.CreateDirectory(path + current);
}@JaredPar的功劳
发布于 2012-06-27 01:39:31
最简单的方法是:
public static void ebfFolderCreate(Object s1)
{
DirectoryInfo di = new DirectoryInfo(s1.ToString());
if (di.Parent != null && !di.Exists)
{
ebfFolderCreate(di.Parent.FullName);
}
if (!di.Exists)
{
di.Create();
di.Refresh();
}
}https://stackoverflow.com/questions/9054952
复制相似问题