生成随机文件名或文件夹

实现效果:

  

知识运用:

  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生成文件夹
            }
        }

  

原文地址:https://www.cnblogs.com/feiyucha/p/10216914.html