生成随机文件名或文件夹

Posted on 2019-01-03 21:02  努力成长静待花开  阅读(1125)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  Guid结构的NewGuid方法  //用来初始化Guid结构的一个新实例

  public static Guid NewGuid ()    //返回值: 新的Guid对象

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog P_fb = new FolderBrowserDialog();
            if (P_fb.ShowDialog() == DialogResult.OK)
            {
                File.Create(P_fb.SelectedPath+"\\"+
                    Guid.NewGuid().ToString()+".txt");  //根据GUID生成文件名
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog P_fb = new FolderBrowserDialog();
            if (P_fb.ShowDialog() == DialogResult.OK)
            {
                Directory.CreateDirectory(P_fb.SelectedPath + "\\" +
                    Guid.NewGuid().ToString());         //根据GUID生成文件夹
            }
        }