资源简介
内容包含C#的各种文件操作,包括文件夹创建、删除,文件创建、删除、移动,有源代码,注释清楚!
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace 实验7_目录与文件操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String path = ““;
String path2 = ““;
String filename = ““;
String movename = ““;
private void button1_Click(object sender EventArgs e)
{
String name=name1.Text;
filename = path + “\\“ + name;
if (Directory.Exists(filename))
MessageBox.Show(“文件夹已经存在,无法创建!“);
else
{
Directory.CreateDirectory(filename);
MessageBox.Show(“文件夹已经创建!“);
}
}
private void button6_Click(object sender EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
path = (String)this.folderBrowserDialog1.SelectedPath;
filepath1.Text = path;
}
}
private void button7_Click(object sender EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
path = (String)this.folderBrowserDialog1.SelectedPath;
Fname1.Text = Path.GetFileName(path);
}
}
private void textBox2_TextChanged(object sender EventArgs e)
{
}
private void filepath1_TextChanged(object sender EventArgs e)
{
}
private void button2_Click(object sender EventArgs e)
{
if(Directory.GetFiles(path).Length +Directory.GetDirectories(path).Length==0)
{
Directory.Delete(path);
MessageBox.Show(“删除成功!“);
}
else
{
if (MessageBox.Show(this “当前文件夹包含子文件夹和文件,将一起删除!\n是,请点击‘确定’\n否则,请点击‘取消‘“ “提示“
MessageBoxButtons.OKCancel MessageBoxIcon.Question) == DialogResult.OK)
{
Directory.Delete(pathtrue);
MessageBox.Show(“删除成功!“);
}
}
}
private void button8_Click(object sender EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
path = (String)this.folderBrowserDialog1.SelectedPath;
filepath2.Text = path;
}
}
private void button3_Click(object sender EventArgs e)
{
String name = name2.Text;
filename = path + “\\“ + name;
if (File.Exists(filename))
MessageBox.Show(“文件已经存在,无法创建!“);
else
{
评论
共有 条评论