Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

CreateFile Sector/Block level equivalent

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott Bell - 30 Aug 2006 23:36 GMT
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
Carl Daniel [VC++ MVP] - 31 Aug 2006 00:50 GMT
> Hello all,
>
[quoted text clipped - 3 lines]
> 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/kernel32/CreateFile.html
http://www.pinvoke.net/default.aspx/kernel32/ReadFile.html

-cd
Willy Denoyette [MVP] - 31 Aug 2006 09:36 GMT
| > Hello all,
| >
[quoted text clipped - 11 lines]
|
| -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.
Scott Bell - 31 Aug 2006 21:41 GMT
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

> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.