| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 9113 人关注过本帖, 2 人收藏
标题:C#如何读取U盘中的文件啊
只看楼主 加入收藏
fjlangzi
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-1-2
收藏(2)
 问题点数:0 回复次数:11 
C#如何读取U盘中的文件啊
我要做一个程序,要求通过USB口读取或者拷贝USB设备中的文件。
请大家不吝赐教!
搜索更多相关主题的帖子: 文件 USB 设备 不吝 
2008-04-08 22:56
zaroty
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-3-28
收藏
得分:0 
不明白啊,你那个程序是在哪里运行啊?计算机上么?那你直接把U盘当作一个硬盘不也可以啊,

http://hi.baidu.com/zaroty  偶滴博客
2008-04-09 09:09
fjlangzi
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-1-2
收藏
得分:0 
回复 2# 的帖子
对是在计算机上运行,不过不会啊
关键是如何读取U盘都有些什么文件啊?
可不可用代码进行说明啊,谢谢了

[[it] 本帖最后由 fjlangzi 于 2008-4-9 16:07 编辑 [/it]]
2008-04-09 13:06
我是土豆
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-10-21
收藏
得分:0 
很像资源管理器哦

其實我懂 ,我在你心中沒有了位置...
2008-04-16 11:29
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 
我记得前段时间有专门查过这方面的资料,USB设备可以通过一个组件来读取,具体的我回去翻翻资料再告诉你。

浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2008-04-16 11:39
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 
找到了……

参考代码如下:

当把U盘放插入,然后程序自动将U盘的内容复制到本地硬盘

using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using

namespace U盘更新
{
    public partial class Form1 : Form
    {
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_CONFIGCHANGECANCELED = 0x0019;
        public const int DBT_CONFIGCHANGED = 0x0018;
        public const int DBT_CUSTOMEVENT = 0x8006;
        public const int DBT_DEVICEQUERYREMOVE = 0x8001;
        public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        public const int DBT_DEVICEREMOVEPENDING = 0x8003;
        public const int DBT_DEVICETYPESPECIFIC = 0x8005;
        public const int DBT_DEVNODES_CHANGED = 0x0007;
        public const int DBT_QUERYCHANGECONFIG = 0x0017;
        public const int DBT_USERDEFINED = 0xFFFF;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case WM_DEVICECHANGE:
                            break;
                        case DBT_DEVICEARRIVAL://U盘插入
                            DriveInfo[] s = DriveInfo.GetDrives();
                            foreach (DriveInfo drive in s)
                            {
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    listBox1.Items.Add("U盘已插入,盘符为:" + drive.Name.ToString());
                                    break;
                                }
                            }
                            break;
                        case DBT_CONFIGCHANGECANCELED:
                            break;
                        case DBT_CONFIGCHANGED:
                            break;
                        case DBT_CUSTOMEVENT:
                            break;
                        case DBT_DEVICEQUERYREMOVE:
                            break;
                        case DBT_DEVICEQUERYREMOVEFAILED:
                            break;
                        case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
                            break;
                        case DBT_DEVICEREMOVEPENDING:
                            break;
                        case DBT_DEVICETYPESPECIFIC:
                            break;
                        case DBT_DEVNODES_CHANGED:
                            break;
                        case DBT_QUERYCHANGECONFIG:
                            break;
                        case DBT_USERDEFINED:
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }

浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2008-04-16 11:41
simpson
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:863
专家分:17
注 册:2006-11-16
收藏
得分:0 
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using

namespace CH1
{
    public partial class CH1_DemoForm010 : Form
    {
        public CH1_DemoForm010()
        {
            InitializeComponent();
        }

        private void CH1_DemoForm010_Load(object sender, EventArgs e)
        {
            DiskDriveComboBox.Items.AddRange(Directory.GetLogicalDrives());
        }

        private void DiskDriveComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DiskDriveComboBox.SelectedIndex != -1)
            {
                try
                {
                    string[] subdirectoryEntries = Directory.GetDirectories((string)DiskDriveComboBox.SelectedItem);
                    DirectoryListBox.DataSource = subdirectoryEntries;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    DirectoryListBox.DataSource = null;
                    txtResult.Clear();
                }
            }
        }

        private void DirectoryListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DirectoryListBox.SelectedIndex != -1)
            {
                // 显示一个状态讯息对话框来表示我们目前要尝试取得所有层级的子目录。
                frmStatus frmStatusMessage = new frmStatus();
                frmStatusMessage.Show("尝试取得所有层级的子目录,处理中,请稍后....");

                // 建立一个对应至使用者所选取之目录的 DirectoryInfo 对象。
                DirectoryInfo theDir = new DirectoryInfo((string)DirectoryListBox.SelectedItem);

                StringBuilder sb = new StringBuilder();

                // 判断目录是否存在。
                if (theDir.Exists)
                {
                    // 显示关于该目录的基本信息。
                    sb.Append("此目录的完整路径是:");
                    sb.Append(theDir.FullName);
                    sb.Append(",它是 ");
                    sb.Append(theDir.Parent.Name);
                    sb.AppendLine(" 的子目录。");

                    sb.Append("根目录:");
                    sb.AppendLine(theDir.Root.Name);

                    sb.Append("属性:");
                    sb.AppendLine(theDir.Attributes.ToString());

                    sb.Append("建立时间:");
                    sb.AppendLine(theDir.CreationTime.ToString());

                    sb.Append("最近一次的访问时间:");
                    sb.AppendLine(theDir.LastAccessTime.ToString());

                    sb.Append("最近一次的写入时间:");
                    sb.AppendLine(theDir.LastWriteTime.ToString());

                    // 以下的程序代码会列出目录的内容。

                    DirectoryInfo[] entries;
                    sb.AppendLine("-------------------------------------------");

                    try
                    {
                        entries = theDir.GetDirectories("*", SearchOption.AllDirectories);
                    }
                    catch
                    {
                        sb.Append("您没有权限来列示此目录。");
                        txtResult.Text = sb.ToString();
                        return;
                    }

                    if (entries.Length > 0)
                    {
                        sb.AppendLine(theDir.FullName + " 之下的所有子目录路径名称");
                        sb.AppendLine("-------------------------------------------");

                        foreach (DirectoryInfo entry in entries)
                        {
                            sb.AppendLine(entry.FullName);
                        }
                    }
                }
                else
                {
                    sb.Append("目录不存在或是您没有权限来查看它。");
                }

                frmStatusMessage.Close();
                txtResult.Text = sb.ToString();
            }
        }
    }
}

1.JPG (70.25 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

全国最大的 Java专业电子书免费分享[url]http:///in.asp?id=xrmao[/url]
2008-04-16 11:42
pacocai
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1583
专家分:0
注 册:2007-3-12
收藏
得分:0 
这个其实是通过Win32 SDK来实现的,详细的可以再参照win32 sdk来编写。

浮生若梦天边月,醉死如酒水中星。红楼一梦千人叹,岂让万夫空做贱。博客:http://hi.baidu.com/rxvip
2008-04-16 11:43
simpson
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:863
专家分:17
注 册:2006-11-16
收藏
得分:0 
回复 6# 的帖子
Good !!

全国最大的 Java专业电子书免费分享[url]http:///in.asp?id=xrmao[/url]
2008-04-16 11:45
fjlangzi
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-1-2
收藏
得分:0 
谢谢各位热心的朋友
2008-04-18 23:56
快速回复:C#如何读取U盘中的文件啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.118639 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved