c # How to generate a folder or rename the file automatically when

If you are inside a folder, file or folder is added continuously (do not change the name), then the system will automatically add (1), (2), (3) ...

This effect can not I search on the Internet, not too difficult to write about myself.

Folder:

 string dir = @"C:\Users\lenovo\Desktop\Test\2";
            string newdir = dir;
            int i = 0;
            while (Directory.Exists(newdir))
            {
                newdir = dir + "(" + i + ")";
                i++;
            }
            Directory.CreateDirectory(newdir);

 

file:

private void btnAdd_Click(object sender, EventArgs e)
        {
             string name = @"d:\KuGouCache\abc.txt";
            int pos = name.LastIndexOf('.');
            name = name.Insert(pos, "({0})");

            int i = 0;
            var newName = string.Format(name, i);
            while (File.Exists(newName))
            {
                newName = string.Format(name, i);
                i++;
            }
            File.Create(newName);
        }
Released six original articles · won praise 189 · views 280 000 +

Guess you like

Origin blog.csdn.net/newbie_xymt/article/details/102918526