//CreateFolder Operation private void button1_Click(object sender, EventArgs e) { string path = @"F:\JunRui"; if (!Directory.Exists(path)) { //创建文件夹,如果没有JunRui文件夹则同时也创建JunRui文件夹 Directory.CreateDirectory(path); this.richTextBox1.Text += "创建成功\r\n"; } else { this.richTextBox1.Text += "此文件夹已存在\r\n"; } //删除文件夹 //如果第二个参数为True,则会删除此目录下的子目录 //Directory.Delete(@"F:\JunRui",true); } //DeleteFolder Operation private void button2_Click(object sender, EventArgs e) { string path = @"F:\JunRui"; if (Directory.Exists(path)) { //删除文件夹 Directory.Delete(path, true); this.richTextBox1.Text += "删除成功\r\n"; } else { this.richTextBox1.Text += "没有发现要删除的文件夹\r\n"; } } //Folder移动 private void button3_Click(object sender, EventArgs e) { string path = @"F:\JunRui\"; if (Directory.Exists(path)) { try { /* * 将F盘下的JunRui文件夹及其子文件夹移动到F盘Debug目录下 * 并将JunRui文件夹重命名为TestMove * 驱动器之间不能移动 */ Directory.Move(path, @"F:\Debug\TestMove"); this.richTextBox1.Text += "移动成功\r\n"; } catch (IOException io) { this.richTextBox1.Text += io.Message.ToString(); } } else { this.richTextBox1.Text += "没有文件可移动\r\n"; } } //Run File private void button4_Click(object sender, EventArgs e) { string path = @"F:\JunRui"; //得到上面目录下所有文件名和扩展名(包括路径信息) string[] files = Directory.GetFiles(path); foreach (string item in files) { //打开文件 //如果是根据文件扩展名,调用相应的软件打开。 //例如:System.Diagnostics.Process.Start("目标软件","要打开的文件"); System.Diagnostics.Process.Start(item); } } //当然不能通过遍历把每个文件都打开,可以通过递归添加到TreeView控件或添加到ListBox控件等等,然后通过相关事件运行要打开的文件。 //Create File private void button5_Click(object sender, EventArgs e) { string path = @"F:\JunRui\CSharpAddFile.txt"; if (!File.Exists(path)) { File.Create(path); this.richTextBox1.Text += "创建成功\r\n"; } else { this.richTextBox1.Text += "此名称的文件已存在\r\n"; } } //读取TXT文件 private void button6_Click(object sender, EventArgs e) { string path = @"F:\JunRui\sjr.txt"; listBox1.DataSource = File.ReadAllLines(path); }