C#winform编程 C#, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 获取磁盘空间,查查API,可以获取的但是找文件夹,就有难度了,估计只能遍历搜索了 BackgroundWorker backgroundWorker1 = new BackgroundWorker(); public Form2() { InitializeComponent(); InitializeBackgroundWorker(); } private void InitializeBackgroundWorker() { backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler( backgroundWorker1_RunWorkerCompleted); } private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { if (null == e.Error) { MessageBox.Show("copy successed!"); } else { MessageBox.Show(e.Error.ToString()); } } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { CopyFolder(@"D:\XML", GetDiskFreeSpaceDrive().Name + "New\\"); } private void button1_Click(object sender, EventArgs e) { if (!backgroundWorker1.IsBusy) { backgroundWorker1.RunWorkerAsync(); } } public DriveInfo GetDiskFreeSpaceDrive() { System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); DriveInfo result = drives[0]; for (int i = 1; i < drives.Length; i++) { if (drives[i].TotalFreeSpace > result.TotalFreeSpace) { result = drives[i]; } } return result; } private static void CopyFolder(string from, string to) { if (!Directory.Exists(to)) Directory.CreateDirectory(to); // 子文件夹 foreach (string sub in Directory.GetDirectories(from)) CopyFolder(sub + "\\", to + Path.GetFileName(sub) + "\\"); // 文件 foreach (string file in Directory.GetFiles(from)) File.Copy(file, to + Path.GetFileName(file), true); }仅供参考!要注意一些参数的判断和文件读写的时候文件的判断以及exception C# 更改圆角/半角输入方式 c#中树的用法 急救啊。。treeview绑定还没有解决。。。。。。。 C#变量类型后面加?是什么意思? combox手动绑定不能刷新 有没有一种方法把 String 类型 转换成 byte[] 类型 ?在线 asp.net如何获得google流量系统的数据? 在web开发中的 <hr> 这一功能在windows开发时是怎么实现的? C/S开发用ADO连接数据库能不能指定客户端的端口号为固定的 在一个解决方案中如何同时使用多个项目 求助:高速串口数据接收 chart连接数据库读取数据,哪里出错了?
但是找文件夹,就有难度了,估计只能遍历搜索了
{
InitializeComponent();
InitializeBackgroundWorker();
} private void InitializeBackgroundWorker()
{
backgroundWorker1.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
}
private void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
if (null == e.Error)
{
MessageBox.Show("copy successed!");
}
else
{
MessageBox.Show(e.Error.ToString());
}
} private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
CopyFolder(@"D:\XML", GetDiskFreeSpaceDrive().Name + "New\\");
} private void button1_Click(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
} public DriveInfo GetDiskFreeSpaceDrive()
{
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); DriveInfo result = drives[0]; for (int i = 1; i < drives.Length; i++)
{
if (drives[i].TotalFreeSpace > result.TotalFreeSpace)
{
result = drives[i];
}
} return result;
} private static void CopyFolder(string from, string to)
{
if (!Directory.Exists(to))
Directory.CreateDirectory(to); // 子文件夹
foreach (string sub in Directory.GetDirectories(from))
CopyFolder(sub + "\\", to + Path.GetFileName(sub) + "\\"); // 文件
foreach (string file in Directory.GetFiles(from))
File.Copy(file, to + Path.GetFileName(file), true);
}仅供参考!要注意一些参数的判断和文件读写的时候文件的判断以及exception