【问题标题】:Adding a number suffix when creating folder in C#在 C# 中创建文件夹时添加数字后缀
【发布时间】:2012-01-29 17:04:29
【问题描述】:

我正在尝试处理我要创建的文件夹是否已经存在 .. 为文件夹名称添加一个数字 .. 像 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);
    }

【问题讨论】:

    标签: c# directory counter


    【解决方案1】:

    为此,您不需要递归,而是应该寻找迭代解决方案

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

    【讨论】:

    • 非常感谢 :) .. mm 我有一个小问题 .. 我使用 treeview 制作了一个文件浏览器 .. 现在我想使用 listview 来做这件事 .. 如何在 listview 中获取 ParentNode.. . 我的意思是当前文件夹.. listviewitem 中是否有选项或者我必须将当前路径保存在字符串中?
    • ListViewItem 对象有一个Tag 属性,您可以在其中存储任意数据。你可以把路径放在这里。
    • cool .. 出于某种我不知道为什么的原因.. 当我给出路径 @"d:" .. 它创建在磁盘 c: .. 为什么??
    • @MurHafSoz 这是一个非常烦人的 Windows 遗留问题。如果您至少不指定反斜杠,它会给您带来意想不到的行为。所以d:` is different than d:`
    • 抱歉我的英语不好......我没明白你的意思,我应该如何通过路径?
    【解决方案2】:
        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 的功劳

    【讨论】:

      【解决方案3】:

      最简单的方法是:

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

      【讨论】:

        【解决方案4】:

        您可以使用此 DirectoryInfo 扩展器:

        public static class DirectoryInfoExtender
        {
            public static void CreateDirectory(this DirectoryInfo instance)
            {
                if (instance.Parent != null)
                {
                    CreateDirectory(instance.Parent);
                }
                if (!instance.Exists)
                {
                    instance.Create();
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-14
          • 2016-01-14
          • 2018-12-08
          • 2017-06-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多