PC Review


Reply
Thread Tools Rate Thread

CreateFile/FileStream parameter incorrect

 
 
Theewara Vorakosit
Guest
Posts: n/a
 
      22nd Jan 2005
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


 
Reply With Quote
 
 
 
 
Patrick Steele [MVP]
Guest
Posts: n/a
 
      23rd Jan 2005
In article <(E-Mail Removed)>, g4685034
@cc.cpe.ku.ac.th says...
> 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()?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
 
Reply With Quote
 
Theewara Vorakosit
Guest
Posts: n/a
 
      23rd Jan 2005
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

"Patrick Steele [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In article <(E-Mail Removed)>, g4685034
> @cc.cpe.ku.ac.th says...
>> 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()?
>
> --
> Patrick Steele
> Microsoft .NET MVP
> http://weblogs.asp.net/psteele



 
Reply With Quote
 
Patrick Steele [MVP]
Guest
Posts: n/a
 
      23rd Jan 2005
In article <emd#(E-Mail Removed)>, g4685034
@cc.cpe.ku.ac.th says...
> 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.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
 
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
FileShare parameter in filestream creation muralidhargvn@hotmail.com Microsoft Dot NET Framework 7 12th Aug 2006 04:42 PM
Passing FileStream as parameter? Tomas Jönsson Microsoft C# .NET 0 25th May 2005 01:15 PM
FileStream GURU Help - Filestream and Icon files .NET SQL2000 Bob Microsoft ADO .NET 1 5th Apr 2004 05:55 PM
FileStream async access and FileStream locks Natalia DeBow Microsoft C# .NET 0 23rd Mar 2004 06:59 PM
FileStream.Lock, FileStream.Unlock and its sync. John Microsoft Dot NET 0 11th Sep 2003 11:05 AM


Features
 

Advertising
 

Newsgroups
 


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