2

私は、コンピューターからのいくつかのファイルで 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);
//==============================================================================
            }
        }
    }
}
4

3 に答える 3

2

方法は次のとおりです

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);
        }
    }
}
于 2012-08-16T12:55:34.293 に答える
1

File.Copyを使用して、宛先にUSBドライブ文字を使用します。例えば:

string sourceDir = @"c:\current";
string backupDir = @"f:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files. 
    foreach (string f in picList)
    {
        // Remove path from the file name. 
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path. 
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

    // Copy text files. 
    foreach (string f in txtList)
    {

        // Remove path from the file name. 
        string fName = f.Substring(sourceDir.Length + 1);

        try
        {
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
        }

        // Catch exception if the file was already copied. 
        catch (IOException copyError)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied. 
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}
于 2012-08-16T12:54:34.210 に答える
0

MSDN を参照してください: http://msdn.microsoft.com/en-us/library/bb762914.aspx

于 2012-08-16T12:57:36.987 に答える