解决方案 »

  1.   

    获取磁盘空间,查查API,可以获取的
    但是找文件夹,就有难度了,估计只能遍历搜索了
      

  2.   

            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