您好,
我们正在开发一个Windows应用程序使用Visual Studio 2005中使用C#。我们有一些迫切要求
基本上,我们需要读取和写入二进制文件(。斌)在记忆卡(线性闪存)。我们将使用一个USB适配器连接记忆卡。卡在浏览器作为移动设备的检测,但没有体积的标签细节。
使用CreateFile处理读操作fine.But写操作的工作,不工作时,我试着写使用CreateFile手柄。不返回AY错误,但二进制数据没有得到改变。
谁能告诉我如何解决这个
下面是正在使用的读取和写入的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management; // need to add System.Management to your project references.
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.IO;
namespace GetUSB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine();
String[] drives = Environment.GetLogicalDrives();
MessageBox.Show(string.Format("GetLogicalDrives: {0}", String.Join(", ", drives)));
this.comboBox1.Items.AddRange(new object[] {
"0",
"1",
"2",
"3",
"4",
"5",
"6"});
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern SafeFileHandle CreateFile(string lpFileName,
int dwDesiredAccess,
int dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
SafeFileHandle hTemplateFile);
internal const int GENERIC_READ = unchecked((int)0x80000000);
internal const int GENERIC_ALL = unchecked((int)0x10000000);
internal const int OPEN_EXISTING = 3;
internal const int FILE_ATTRIBUTE_NORMAL = 0x80;
protected void ReadRawFile(int DriveNumber)
{
const long MAX_FILE_SIZE = 4194304;
//const long MAX_FILE_SIZE = 1024;
SafeFileHandle h = null;
h = CreateFile("\\\\.\\PhysicalDrive" + DriveNumber.ToString(), GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, new SafeFileHandle(IntPtr.Zero, true));
Cursor.Current = Cursors.WaitCursor;
if (!h.IsInvalid)
{
FileStream stream = new FileStream(h, FileAccess.Read);
//if (!string.IsNullOrEmpty(sfDialog.FileName))
//{
FileStream outputFile = new FileStream(@"C:\\Temp\\Test" + DriveNumber.ToString() + ".bin", FileMode.Create);
//Application.UseWaitCursor = true;
//Cursor.Current = Cursors.WaitCursor;
Application.DoEvents();
try
{
long LocationCtr = 0;
while (LocationCtr < MAX_FILE_SIZE)
{
//Application.DoEvents();
outputFile.WriteByte((byte)stream.ReadByte());
LocationCtr++;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
outputFile.Close();
stream.Close();
//Cursor.Current = Cursors.Default;
Application.DoEvents();
//}
}
else
{
// get error code and throw
int error = Marshal.GetLastWin32Error();
}
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int GetLogicalDrives();
private void button2_Click(object sender, EventArgs e)
{
for (int Index = 0; Index < 10; Index++)
{
ReadRawFile(Index);//Convert.ToInt32(comboBox1.SelectedValue));
}
}
private void button3_Click(object sender, EventArgs e)
{
const long MAX_FILE_SIZE = 4096;
//const long MAX_FILE_SIZE = 1024;
SafeFileHandle h = null;
h = CreateFile("\\\\.\\PhysicalDrive" + "1", GENERIC_ALL, 2, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, new SafeFileHandle(IntPtr.Zero, true));
Cursor.Current = Cursors.WaitCursor;
if (!h.IsInvalid)
{
FileStream stream = new FileStream(h, FileAccess.Write);
FileStream stream1 = new FileStream("C:\\Temp\\Test1.Bin", FileMode.Open);
try
{
char ch = 't';
long LocationCtr = 0;
while (LocationCtr < MAX_FILE_SIZE)
{
byte b = Convert.ToByte(ch);// 0;
stream.WriteByte(b);// ((byte)stream1.ReadByte());
LocationCtr++;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
else
{
// get error code and throw
int error = Marshal.GetLastWin32Error();
}
h.Close();
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
private string _DeviceID;
private string _PnpDeviceID;
private string _Description;
public string DeviceID { get { return _DeviceID; } set { _DeviceID = value; } }
public string PnpDeviceID { get { return _PnpDeviceID; } set { _PnpDeviceID = value; } }
public string Description { get { return _Description; } set { _Description = value; } }
}
}
的问候,
nidheesh