gpt4 book ai didi

c# - 如何将文件夹复制到 U 盘? C#

转载 作者:行者123 更新时间:2023-11-30 22:26:14 26 4
gpt4 key购买 nike

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

该程序在启动时运行并监控插入计算机的任何 USB 或 CD。我的程序是将一些文件夹及其文件复制到 USB。我在将文件夹复制到 USB 时遇到问题,希望得到一些帮助,谢谢。

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);
//==============================================================================
}
}
}
}

最佳答案

Here是如何做到的:

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);
}
}
}

关于c# - 如何将文件夹复制到 U 盘? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987230/

26 4 0