|
-
March 13th, 2009, 10:39 AM
#1
unable to write at the specified file pointer
hey guys i m trying to write at a specified sector on a fat32 drive..
i m able to read from any sector by setfilepointer but wheen i write after moviing the pointer it still writes on the starting of the drive at 0 sector how can i write at any sectorr can any on help me plzzzzzz its very important very very ....
im totally stuck
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32.SafeHandles;
namespace FileLock
{
struct fat32
{
}
class fl
{
public int root = 0;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const uint FILE_SHARE_DELETE = 0x00000004;
public const uint FILE_FLAG_WRITE_THROUGH = 0x80000000;
public const uint FILE_FLAG_OVERLAPPED = 0x40000000;
public const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
public const uint FILE_FLAG_RANDOM_ACCESS = 0x10000000;
public const uint FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000;
public const uint FILE_ATTRIBUTE_NORMAL = 0x80;
public const short INVALID_HANDLE_VALUE = -1;
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint CREATE_NEW = 1;
public const uint CREATE_ALWAYS = 2;
public const uint OPEN_EXISTING = 3;
public enum EMoveMethod : uint
{
Begin = 0,
Current = 1,
End = 2
}
// [DllImport("coredll")]
//static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
// out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern unsafe uint SetFilePointer(
[In] SafeFileHandle hFile,
[In] int lDistanceToMove,
[Out] int* lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern unsafe uint SetFilePointerB(
[In] IntPtr File,
[In] int lDistanceToMove,
[Out] int* lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint SetFilePointer(
[In] SafeFileHandle hFile,
[In] int lDistanceToMove,
[Out] out int lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);
[DllImport("kernel32.dll")]///////////////Read File/////////////
static extern bool ReadFile(SafeFileHandle hFile, byte[] lpBuffer,
uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll")]//////writeFile function/////////////
static extern bool WriteFile(SafeFileHandle hFile, byte[] lpBuffer,
uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten,
[In] ref System.Threading.NativeOverlapped lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]///////CreateFile////////////
static extern IntPtr CreateFileA(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern SafeFileHandle CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr SecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile
);
public SafeFileHandle ptr;
public SafeFileHandle Connect(String a)
{
return CreateFile(a, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, IntPtr.Zero);
}
public string ReadString(Byte[] buff)
{
String temp="";
for (int i = 0; i < buff.Length; i++)
{
if (i % 15 == 0)
temp += "\n";
temp += String.Format("{0:X}", (int)buff[i]) + " ";
//temp += buff[i]+"__";
}
return temp;
}
public Byte[] read(SafeFileHandle ptr)
{
Byte[] Buff = new System.Byte[512];
FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
lpt.Read(Buff, 0, Buff.Length);
return Buff;
}
public int RootStart(Byte[] buff)
{
string temp ="";
temp += String.Format("{0:X}", (int)buff[15]) + String.Format("{0:X}", (int)buff[14]);
int reserved = Convert.ToInt32(temp,16);
int nof = buff[16];
int secpF=0;
if (buff[22] == 0)
{
temp = "";
temp += String.Format("{0:X}", (int)buff[39]) + String.Format("{0:X}", (int)buff[38]) + String.Format("{0:X}", (int)buff[37]) + String.Format("{0:X}", (int)buff[36]);
secpF = Convert.ToInt32(temp, 16);
}
else
{
secpF=Convert.ToInt32(((int)buff[22])+"",16);
}
return secpF*nof+reserved;
}
public unsafe void toRoot(SafeFileHandle ptr, int sector)
{
SetFilePointer(ptr,(sector*512),null,0 );
}
public unsafe string fileCheck(String fname,out int sector)
{
//toRoot(ptr, RootStart(read(ptr)));
int sec=-1;
string temp="";
string temp2="";
//temp = filename(read(ptr), "",i, out sec);
Byte[] b = new byte[512];
b=read(ptr);
// temp = filename(b, "", 0, out sec);
//temp2 += temp + " " + fname + string.Compare(temp, fname, true);
for (int i =0; i<15; i++)
{temp = filename(b, "",0, out sec);
if (string.Compare(temp, fname, true) == 0)
{ sector = sec; temp2 = "found";
b[sec] = 40;
uint x;
System.Threading.NativeOverlapped g=new System.Threading.NativeOverlapped();
SetFilePointer(ptr, 512*root, null, 0);
ReadFile(ptr,b, 512, out x, IntPtr.Zero);
b[11] = 40;
WriteFile(ptr, b, 512, out x, ref g);
break;
}
else
temp2+=temp+ "_____";
// temp2 += temp + "___";
}
sector = sec;
return temp2;
}
public void reset()
{
ptr = Connect("\\\\.\\N:");
}
public string filename(byte[] buff, string s,int no,out int sec)
{
if (buff[0 + 32 * no] != 229 && buff[0 + 32 * no] != 00 && buff[0 + 32 * no] != 05)
{
if (buff[11 + 32 * no] != 15)
{
String temp = "";
int i = 32 * no; int c = 0;
while (buff[i] != 32 && c < 8)
{
c++;
temp += (char)buff[i++] + "";
}
temp += ".";
temp += (char)buff[8 + 32 * no]; temp += (char)buff[9 + 32 * no]; temp += (char)buff[10 + 32 * no];
sec = (11 + 32 * no);
return temp;
}
else
{
string temp = "";
int c = buff[0 + 32 * no] - 64;
if (c > 0)
{
int[] rec = new int[] { 1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30 };
for (int i = c; i > 0; i--)
{
foreach (int j in rec)
{
if (buff[j + 32 * (i - 1) + 32 * no] != 255)
temp += (char)buff[j + 32 * (i - 1) + 32 * no];
}
}
}
else
temp = "LNF";
sec = (11+32 * (c + 1) + 32 * no);
return temp;
}
}
else
{
sec = 0;
return "error";
}
}
public fl()
{
ptr = Connect("\\\\.\\N:");
root = RootStart(read(ptr));
}
}
}
class con
{
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|