Hi,
We are developing a Windows application using Visual Studio 2005 using C#. We have some urgent requirement
Basically we need to read and write a binary (.bin) file in to a memory card(Linear flash). We will connect the memory card using a USB Adapter. The card is detecting in explorer as removable device, but no volumn label details are available.
Using CREATEFILE handle the READ operation is working fine.But the Write operation is not working when i try to write using the CREATEFILE handle. Its it not returning ay errors, but the binary data is not getting changed.
Can anyone tell me how to solve this
Below is the Code which am using for READ and Write
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;
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;
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);
FileStream outputFile = new FileStream(@"C:\\Temp\\Test" + DriveNumber.ToString() + ".bin", FileMode.Create);
Application.DoEvents();
try
{
long LocationCtr = 0;
while (LocationCtr < MAX_FILE_SIZE)
{
outputFile.WriteByte((byte)stream.ReadByte());
LocationCtr++;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
outputFile.Close();
stream.Close();
Application.DoEvents();
}
else
{
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);
}
}
private void button3_Click(object sender, EventArgs e)
{
const long MAX_FILE_SIZE = 4096;
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);
stream.WriteByte(b);
LocationCtr++;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
else
{
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; } }
}
}
Regards,
Nidheesh