CreateFile/FileStream parameter incorrect

T

Theewara Vorakosit

Hello,

I use CreateFile (p/invoke) to open my drive, which is a ramdisk. Then I
take a handle to FileStream constructor. There is an exception occur at
FileStream constructor. Here is my code.

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFile(
string filename,
[MarshalAs(UnmanagedType.U4)] FileAccess fileaccess,
[MarshalAs(UnmanagedType.U4)] FileShare fileshare,
int securityattributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationdisposition,
int flags, IntPtr template);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
IntPtr hDrv = CreateFile(
"\\\\.\\g:",
FileAccess.Read,
FileShare.ReadWrite,
0,
FileMode.Open,
0,
IntPtr.Zero);

FileStream fs = new FileStream(hDrv, FileAccess.Read);

Thereis IOException occur with "The parameter is incorrect." However, the
hDrv parameter can be used correctly with ReadFile (P/invoke).
Is this a limitation?

Thanks,
Theewara
 
P

Patrick Steele [MVP]

g4685034 said:
I use CreateFile (p/invoke) to open my drive, which is a ramdisk. Then I
take a handle to FileStream constructor. There is an exception occur at
FileStream constructor.

Any reason you don't use the managed System.IO.File.Create()?
 
T

Theewara Vorakosit

I want to open physical device, g:. I try it and the
System.ArgumentException occors. It said "FileStream will not open Win32
devices such as disk partitions and tape drives. Don't use \\.\ in your
path.
So, I use p/invoke.
Theewara
 
P

Patrick Steele [MVP]

g4685034 said:
I want to open physical device, g:. I try it and the
System.ArgumentException occors. It said "FileStream will not open Win32
devices such as disk partitions and tape drives. Don't use \\.\ in your
path.
So, I use p/invoke.

I see. Have you checked out http://www.pinvoke.net to see that your
signature is correct? There may be some sample code there too that
might help get you going.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top