如何将文件夹复制到USB? C# [英] how do I copy a folder to a USB? C#

查看:103
本文介绍了如何将文件夹复制到USB? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,该项目将使用计算机中的某些文件自动更新USB。

I'm working on a project that will automatically update my USB with some files from my computer.

该程序可在启动时运行,并监视插入计算机的任何USB或CD。我的程序是然后将一些文件夹及其文件复制到USB。我在将文件夹复制到USB时遇到麻烦,谢谢您。

The program works on start up and monitors for any USB or CD that is plugged into the computer. My program is to then copy some folders and its files to the USB. I am having trouble copying the folders into the USB and would appreciate some help, thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;




namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

 // this section starts the timer so it can moniter when a USB or CD is inserted into
 // the computer.    
//==================================================================================
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 100;
            timer1.Start();

            WindowState = FormWindowState.Minimized;
//===================================================================================            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

 // this section checks to see if there is a drive type of USB and CDs.          

            foreach(DriveInfo drive in DriveInfo.GetDrives())
            {
                if (drive.DriveType == DriveType.Removable)
                {
// this part is supposed to copy a folder from the PC and paste it to the USB
//==============================================================================                    

//==============================================================================                   
                }

                if (drive.DriveType == DriveType.CDRom)
                {
// same thing but for CDs.
//==============================================================================

//==============================================================================
                }
            }


        }
// this section opens a folderbrowserdialog that the users can use to access their folders 
//and put them into a listbox so when a USB or CD is inserted it will copy those files into
// the storage devices.
//==============================================================================
        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Add(folderBrowserDialog1.SelectedPath);
//==============================================================================
            }
        }
    }
}


推荐答案

此处是可以完成的操作:

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
    DirectoryInfo dir = new DirectoryInfo(sourceDirName);
    DirectoryInfo[] dirs = dir.GetDirectories();

    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

    if (!Directory.Exists(destDirName))
    {
        Directory.CreateDirectory(destDirName);
    }

    FileInfo[] files = dir.GetFiles();
    foreach (FileInfo file in files)
    {
        string temppath = Path.Combine(destDirName, file.Name);
        file.CopyTo(temppath, false);
    }

    if (copySubDirs)
    {
        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(destDirName, subdir.Name);
            DirectoryCopy(subdir.FullName, temppath, copySubDirs);
        }
    }
}

这篇关于如何将文件夹复制到USB? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆