PDA

View Full Version : خواندن از دیسک



zahra faraji
دوشنبه 19 دی 1390, 09:11 صبح
سلام دوستان
این چهارمین باره که سوال میکنم. ببینید من میخوام یک قسمت خاصی از یک دیسک رو بخونم. برای این کار از createfile و readfile و setfilepointer استفاده کردم. اینم کدم:



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.Runtime.InteropServices;
using System.Windows.Forms;


namespace BenchmarkDiscoDuro
{
public partial class Form1 : Form
{
private const short FILE_ATTRIBUTE_NORMAL = 0x80;
private const short INVALID_HANDLE_VALUE = -1;
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint CREATE_NEW = 1;
private const uint CREATE_ALWAYS = 2;
private const uint OPEN_EXISTING = 3;
private const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
private const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
private const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
private const uint FILE_FLAG_WRITE_THROUGH = 0x80000000;//para que escriba directamente en el disco

private const int FILE_BEGIN = 0;
private const int FILE_CURRENT = 1;
private const int FILE_END = 2;
static Timer timer1 = new Timer();
static int SIZE_FILE;// = tamanio * 1024 * 1024;
static byte[] buffer;// = new byte[SIZE_FILE];
static int m_handleValue;

[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile
);

[DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer,
int nNumberofBytesToWrite, ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped);

[DllImport("kernel32.dll")]
private static extern bool ReadFile(int hFile, byte[] lpBuffer,
int nNumberOfBytesToRead, ref int lpNumberOfBytesReaded,
ref OVERLAPPED lpOverlapped);

[DllImport("kernel32.dll")]
private static extern int SetFilePointer(int hFile,
int lDistanceToMove, ref int lpDistanceToMoveHigh,
int dwMoveMethod);

[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hFile);

public Form1()
{

InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
Read_Random(Int32.Parse(textBox1.Text));
}

public void Read_Random(int size)
{
SIZE_FILE = size * 1024 * 1024;
buffer = new byte[1024];

int vAux = SIZE_FILE;
bool resultado = false;
OVERLAPPED ovlRead = new OVERLAPPED();
int[] pos = new int[SIZE_FILE / 1024];
Random posRandom = new Random();
// INICIALIZACIONES
for (int i = 0; i < (SIZE_FILE / 1024); i++)
{
pos[i] = posRandom.Next(SIZE_FILE - 1024);
}
m_handleValue = CreateFile("\\\\.\\PhysicalDrive1", GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, (uint)FILE_FLAG_NO_BUFFERING, IntPtr.Zero);
if (m_handleValue != -1)
MessageBox.Show("connected...");

for (int i = 0; i < (SIZE_FILE / 1024); i++)
{
SetFilePointer(m_handleValue, pos[i], ref SIZE_FILE,
FILE_BEGIN);
resultado = ReadFile(m_handleValue, buffer, 1024, ref vAux, ref ovlRead);
richTextBox1.AppendText( "buffer[0]: " + buffer[0].ToString("X2") + "\r\n");
}



resultado = CloseHandle(m_handleValue);
}

public void Read_Sequencial(int size)
{
SIZE_FILE = size * 1024 * 1024;
buffer = new byte[SIZE_FILE];
int leidos = 0;
bool resultado = false;
OVERLAPPED ovlRead = new OVERLAPPED();
m_handleValue = CreateFile(
"\\\\.\\PhysicalDrive1",
GENERIC_READ,
0,
IntPtr.Zero,
OPEN_EXISTING,
(uint)FILE_FLAG_NO_BUFFERING,
IntPtr.Zero);
resultado = ReadFile(m_handleValue, buffer, SIZE_FILE, ref leidos, ref ovlRead);
resultado = CloseHandle(m_handleValue);
foreach (byte b in buffer)
{
richTextBox1.AppendText(b.ToString("X2")+"\r\n");
}
}

private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
Read_Sequencial(Int32.Parse(textBox1.Text));
}
}

}


ولی نمیدونم چرا بافری که پر میکنه همون 1024 بایت اوله.
تورو خدا کمممممممممممممممممممک:گری :