Open diskpartition

  • Thread starter Thread starter Yves Dhondt
  • Start date Start date
Y

Yves Dhondt

Something went wrong with my previous post, so this is the full question :

Hello,

in the past I used the CreateFile, ReadFile and WriteFile api to access
a diskpartition directly (e.g. "\\.\c:") to read raw sectors. I was
planning on converting some of those old c++ classes to c#. However
using FileStream I get the following error :

"Additional information: FileStream opent geen Win32-apparaten zoals
schijfpartities en tapestations. Maak geen gebruik van \\.\ in het pad."

which translates into

"Additional information: FileStream doesn't open Win32-systems like
diskpartitions and tapestations. Don't use \\.\ in the path."

So I need to use another class but I can't find out which one. Or isn't
there one and am I still stuck with the old win32 API which I need to
access through P/Invoke?

TIA

Yves
 
Yves,

You can still use the FileStream class. What you want to do is call the
CreateFile API function to get a handle to the partition. Once you do that,
you can pass the handle to the constructor of the FileStream object, and
then use it as you would normally.

Hope this helps.
 
Thanks for your reply, I tried doing that but I keep running into a
System.IO.IOException when I try to create the FileStream complaining
about an invalid parameter. I attached the example code below.

Also as a side note, the CreateFile doesn't seem to like volumes (hard
disk). Disk stations, cd/dvd-drives and all other kind of media work all
right (using tests with ReadFile API in stead of the FileStream).

Yves

======== CODE LISTING BEGIN ========

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

class Test {
[DllImport("kernel32", SetLastError=true)]
static extern unsafe IntPtr CreateFile(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
uint SecurityAttributes, // Security Attributes
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
int hTemplateFile // handle to template file
);

public static int Main(string[] args)
{
String the_drive = @"\\.\A:"; // DOESN'T WORK WITH C-DRIVE
IntPtr handle = CreateFile(
the_drive,
0x80000000 | 0x40000000, // GENERIC_READ | GENERIC_WRITE
0x1 | 0x2, // FILE_SHARE_READ | FILE_SHARE_WRITE
0,
3, // OPEN_EXISTING
0,
0);

FileStream fs = new FileStream(handle, FileAccess.ReadWrite);

return 0;
}
}

======== CODE LISTING END ========
 
Yves said:
Thanks for your reply, I tried doing that but I keep running into a
System.IO.IOException when I try to create the FileStream complaining
about an invalid parameter. I attached the example code below.

Also as a side note, the CreateFile doesn't seem to like volumes (hard
disk). Disk stations, cd/dvd-drives and all other kind of media work all
right (using tests with ReadFile API in stead of the FileStream).

I solved this one. The ones I tested it with didn't have FILE_SHARE_READ
| FILE_SHARE_WRITE constants set correctly.
Yves

======== CODE LISTING BEGIN ========

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

class Test {
[DllImport("kernel32", SetLastError=true)]
static extern unsafe IntPtr CreateFile(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
uint SecurityAttributes, // Security Attributes
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
int hTemplateFile // handle to template file
);

public static int Main(string[] args)
{
String the_drive = @"\\.\A:"; // DOESN'T WORK WITH C-DRIVE
IntPtr handle = CreateFile(
the_drive,
0x80000000 | 0x40000000, // GENERIC_READ | GENERIC_WRITE
0x1 | 0x2, // FILE_SHARE_READ | FILE_SHARE_WRITE
0,
3, // OPEN_EXISTING
0,
0);

FileStream fs = new FileStream(handle, FileAccess.ReadWrite);

return 0;
}
}

======== CODE LISTING END ========
Yves,

You can still use the FileStream class. What you want to do is
call the
CreateFile API function to get a handle to the partition. Once you do
that,
you can pass the handle to the constructor of the FileStream object, and
then use it as you would normally.

Hope this helps.
 
Back
Top