PC Review


Reply
Thread Tools Rate Thread

CreateFile Sector/Block level equivalent

 
 
Scott Bell
Guest
Posts: n/a
 
      30th Aug 2006
Hello all,

What is the equivalent in the .NET Framework for accessing
a device at the block/sector level such as one would when
using CreateFile on a device like "\\.\D:"? Attempting to create
a BinaryReader on a FileStream opened (read-only) on "D:"
yields an Access Denied exception.

Thanks,

- Scott

 
Reply With Quote
 
 
 
 
Carl Daniel [VC++ MVP]
Guest
Posts: n/a
 
      31st Aug 2006
Scott Bell wrote:
> Hello all,
>
> What is the equivalent in the .NET Framework for accessing
> a device at the block/sector level such as one would when
> using CreateFile on a device like "\\.\D:"? Attempting to create
> a BinaryReader on a FileStream opened (read-only) on "D:"
> yields an Access Denied exception.


Use P/Invoke to call CreateFile and ReadFile directly passing in the
appropriate options to open a device.

http://www.pinvoke.net/default.aspx/...reateFile.html
http://www.pinvoke.net/default.aspx/.../ReadFile.html

-cd


 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      31st Aug 2006

"Carl Daniel [VC++ MVP]" <(E-Mail Removed)>
wrote in message news:(E-Mail Removed)...
| Scott Bell wrote:
| > Hello all,
| >
| > What is the equivalent in the .NET Framework for accessing
| > a device at the block/sector level such as one would when
| > using CreateFile on a device like "\\.\D:"? Attempting to create
| > a BinaryReader on a FileStream opened (read-only) on "D:"
| > yields an Access Denied exception.
|
| Use P/Invoke to call CreateFile and ReadFile directly passing in the
| appropriate options to open a device.
|
| http://www.pinvoke.net/default.aspx/...reateFile.html
| http://www.pinvoke.net/default.aspx/.../ReadFile.html
|
| -cd
|
|

Note that there is no need to Pinvoke Win32's ReadFile, the handle (use a
SafeHandle in V2) returned by CreateFile can be used to create a FileStream,
this class has a number of contructor overloads that take an IntPtr
(representing the handle) or a SafeHandle.

Willy.


 
Reply With Quote
 
Scott Bell
Guest
Posts: n/a
 
      31st Aug 2006
Willy,

Thanks, the following worked out to be the most ideal:

[DllImport("Kernel32.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern SafeFileHandle CreateFile(
string fileName,
[MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
[MarshalAs(UnmanagedType.U4)] FileShare fileShare,
int securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
int flags,
IntPtr template);

- Scott

Willy Denoyette [MVP] wrote:
> Note that there is no need to Pinvoke Win32's ReadFile, the handle (use a
> SafeHandle in V2) returned by CreateFile can be used to create a FileStream,
> this class has a number of contructor overloads that take an IntPtr
> (representing the handle) or a SafeHandle.
>
> Willy.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read/Write to storage class device using sector/block addresses Arnie Windows XP Embedded 0 21st Apr 2008 09:42 PM
low-level sector copy IDE0 -> IDE1 surface9 Storage Devices 5 5th Aug 2007 08:21 PM
AfterUpdate functional equivalent at Cell level? PeteCresswell Microsoft Excel Programming 2 27th Feb 2004 03:27 PM
Question for disk sector-level I/O operations? Frank Microsoft Windows 2000 File System 9 16th Dec 2003 05:34 AM
Boot Sector Virus - Can I check it on DOS level - Can't boot in Win Daniel Windows XP Hardware 1 28th Oct 2003 04:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:43 AM.