اینا توابعی هست که جواب نداد. البته چندین نوع دیگه هم بود که بی نتیجه بودن.
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Security;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Program_uSD
{
class uSDFunctions
{
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;
private const uint OPEN_EXISTING = 3;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileA(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
private System.IO.FileStream _DriverStream;
private long _SectorLength = 0;
private SafeFileHandle _DriverHandle;
/// <summary>
/// The number of sectors
/// </summary>
public long SectorLength { get { return _SectorLength; } }
/// <summary>
/// Access to the disk sector information
/// </summary>
/// <param name="DriverName">G:</param>
public bool uSD_Init(string DriverName)
{
if (DriverName == null && DriverName.Trim().Length == 0) return false;
_DriverHandle = CreateFileA("\\\\.\\" + DriverName.Trim(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
_DriverStream = new System.IO.FileStream(_DriverHandle, System.IO.FileAccess.ReadWrite);
GetSectorCount();
return true;
}
/// <summary>
/// Gets the number of sectors
/// </summary>
private void GetSectorCount()
{
if (_DriverStream == null) return;
_DriverStream.Position = 0;
byte[] ReturnByte = new byte[512];
_DriverStream.Read(ReturnByte, 0, 512); //Gets the first sector
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x58) //DOS seemed to be 32
{
_SectorLength = (long)BitConverter.ToInt32(new byte[] { ReturnByte[32], ReturnByte[33], ReturnByte[34], ReturnByte[35] }, 0);
}
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x52) //NTFS seems to be 64
{
_SectorLength = BitConverter.ToInt64(new byte[] { ReturnByte[40], ReturnByte[41], ReturnByte[42], ReturnByte[43], ReturnByte[44], ReturnByte[45], ReturnByte[46], ReturnByte[47] }, 0);
}
}
public byte[] uSD_ReadSector(long sectornumber)
{
byte[] SecBuffer = new byte[520];
_DriverStream.Seek(sectornumber * 512, SeekOrigin.Begin);
_DriverStream.Read(SecBuffer, 0, 512);
return SecBuffer;
}
public bool uSD_WriteSector(long sectornumber,byte[] buffer)
{
try
{
byte[] rdbuf = new byte[1000];
int i;
_DriverStream.Seek(sectornumber * 512, SeekOrigin.Begin);
_DriverStream.Write(buffer, 0, 512);
_DriverStream.Seek(sectornumber * 512, SeekOrigin.Begin);
_DriverStream.Read(rdbuf, 0, 512);
for (i = 0; i < 512; i++)
if (buffer[i] != rdbuf[i])
{
string ge = "Error in sector :" + sectornumber.ToString();
MessageBox.Show(ge);
return false;
}
return true;
}
catch
{
return false;
}
}
}
}