C#的System.IO.Directory可以新建目录、删除目录、移动目录、判断目录是否存在。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCreate_Click(object sender, EventArgs e)
{
string dir = Environment.CurrentDirectory + @"\test";
string dest = Environment.CurrentDirectory + @"\dest";
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
MessageBox.Show("目录创建成功!");
}
else
{
MessageBox.Show("目录已存在!");
}
if (Directory.Exists(dir))
{
if (Directory.Exists(dest))
Directory.Delete(dest);
Directory.Move(dir, dest);
}
MessageBox.Show("目录创建时间:" + Directory.GetCreationTime(dest).ToString());
}
}
}
参考:
https://learn.microsoft.com/zh-cn/dotnet/api/system.io.directory?view=net-6.0