使用C#将文件复制到目录 [英] Copying files to a directory using C#

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

问题描述

我正在尝试使用Windows窗体C#和.NetFramework创建一个从cd / dvd复制文件的应用程序。该应用程序将检测DVD / CD,在桌面上创建一个文件夹,并使用cd / dvd的名称命名该文件夹。然后能够将DVD / CD的内容复制到桌面上的文件夹。



我可以帮忙,因为我似乎无法找到方法创建文件夹并命名它。



到目前为止,我已经知道驱动器中是否有CD / DVD,这是我到目前为止所获得的:

I am trying to create an application to copy files from a cd/dvd, using a windows form C# and the .NetFramework. The application is to detect the dvd/cd, create a folder on the desktop and name the folder with the cd/dvd''s name. Then to be able to copy the contents of the dvd/cd to the folder on the desktop.

I could do with help please as I cant seem to find a way to create the folder and name it.

I have got it so far to detect whether there is a cd/dvd in the drive, this is what I have got so far:

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

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

                foreach (var drive in DriveInfo.GetDrives()
                .Where(d => d.DriveType == DriveType.CDRom))
                MessageBox.Show(drive.Name + " " + drive.IsReady.ToString());

                  
         }
    }
} 



我也在看(一旦这个工作)推杆这可以作为计时器或Windows服务(如果可能的话),所以当插入光盘时,它将运行代码。



任何帮助将不胜感激! :)


I am also looking at (once this is working) putting this into a timer or as a windows service (if possible) so when ever a disc is inserted it will run the code.

Any help would be appreciated thanks! :)

推荐答案

System.IO.File.Copy就是你所需要的。和System.IO.Directory.CreateDirectory,System.IO.Directory.GetFiles等一起查找文件。
System.IO.File.Copy is what you need. And System.IO.Directory.CreateDirectory, System.IO.Directory.GetFiles, etc to find files.


Hello Glen,

关于你的第二个问题:创建一个轮询线程,用于检查IsReady属性,然后(例如每10秒)触发一个事件,将就绪状态传递给事件。

在线程方法中特别注意正确的异常处理!

干杯

Andi
Hello Glen,
regarding your second question: create a polling thread to check the IsReady property very now and then (e.g. every 10 seconds) and trigger an event passing the ready status to the event.
Take special care of proper exception handling within the thread method!
Cheers
Andi


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

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