如何创建多个文件夹,就如在桌面上新建第一个时,名为“新建 文本文档.txt”,新建第二个时,名为“新建 文本文档.txt(1)”,新建第三个时,名为“新建 文本文档.txt(3)”,当我删除第一个“新建 文本文档.txt”,再重新创建一个时,其名“新建 文本文档.txt”,该怎么做呢?

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace DataGridViewSampleExm
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();            if(folderBrowserDialog.ShowDialog()==DialogResult.OK)
                {
                    this.textBox1.Text = folderBrowserDialog.SelectedPath;
                }
            }        /// <summary>
            /// 创建文件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                string currPath = this.textBox1.Text.Trim();            if(!File.Exists(Path.Combine(currPath,"新建 文本文档.txt")))
                {
                    File.Create(Path.Combine(currPath, "新建 文本文档.txt"));
                    return;
                }            string[] directoryInfos = Directory.GetFiles(currPath, "正则表达式,这不怎么会,问别人吧",SearchOption.TopDirectoryOnly);            //接下来,去最大的那个数+1            //创建文本File.Create(Path.Combine(currPath, "新建 文本文档.txt(i)"));
            }
        }
    }
      

  2.   

    int value=0;
    string fileName;
    bool key =false;while(key==false)
    {
    if(value==0)
    {
    fileName="新建文件夹.txt";
    }
    else
    {
      fileName="新建文件夹("+value+").txt";
    }if(File.Exist(fileName)
    {
        value++;
        key=false;
    }
    else
    {
       File.Create();
       key=true;
    }
      

  3.   

    判断if (System.IO.Directory.Exists(savePath)){} 是否存在。获取文件夹数
      

  4.   

    按钮单击事件建立~~~经过测试~~~~
    新建文件 副本; 新建文件 副本 副本;新建文件 副本 副本 副本;
    Windows 7 的效果 private void bnt_Click(object sender, EventArgs e)
    {
                string listVs = 路径名;
                bool w = true;
                string x = "新建文件";
                string s = " 副本";
                while (w == true)
                {
                    if (File.Exists(listVs + "\\" + x + ".txt") == true)
                    {
                        x += s;
                        w = true;
                    }
                    else
                    {
                        try
                        {
                            File.CreateText(listVs + "\\" + x + ".txt");
                            w = false;
                        }
                        catch (Exception)
                        {
                            w = true;
                        }
                    }
                }
                GetListViewItem(listVs, imageList2, listView1);
                for (int i = 0; i < listView1.Items.Count; i++ )
                {
                    if (listView1.Items[i].Text == x + ".txt")
                    {
                        listView1.Items[i].BeginEdit();
                        break;
                    }
                   
               }
              
    }
      

  5.   

    按钮单击事件建立~~~经过测试~~~~
    新建文件 副本; 新建文件 副本 副本;新建文件 副本 副本 副本;
    Windows 7 的效果
     
     private void bnt_Click(object sender, EventArgs e)
    {
                string listVs = 路径名;
                bool w = true;
                string x = "新建文件";
                string s = " 副本";
                while (w == true)
                {
                    if (File.Exists(listVs + "\\" + x + ".txt") == true)
                    {
                        x += s;
                        w = true;
                    }
                    else
                    {
                        try
                        {
                            File.CreateText(listVs + "\\" + x + ".txt");
                            w = false;
                        }
                        catch (Exception)
                        {
                            w = true;
                        }
                    }
                }
                //GetListViewItem(listVs, imageList2, listView1);
                //for (int i = 0; i < listView1.Items.Count; i++ )
                //{
                //    if (listView1.Items[i].Text == x + ".txt")
                //    {
                //        listView1.Items[i].BeginEdit();
                //        break;
                //    }
                   
                //}
              
    }