UPDATED QUESTION
Since the ctor is not supported by .NETCF (public FileStream(IntPtr handle, FileAccess access). Could you please suggest other ways of sharing large file in memory between managed and unmanaged code on a limited resource (RAM) platform. Basically I want to map the file in the upper region of 2GB user space (Win CE 5.0) outside of process space / heap. How can I do that in C#.
由于.NETCF不支持ctor(公共FileStream(IntPtr句柄,FileAccess访问)。你能否建议在有限资源(RAM)平台上的托管代码和非托管代码之间共享内存大文件的其他方法。基本上我想要将文件映射到进程空间/堆外部的2GB用户空间(Win CE 5.0)的上部区域。如何在C#中执行此操作。
Also, do MemoryStream objects allocate space in heap or in memory mapped region on Win CE 5.0 ?
另外,MemoryStream对象是否在Win CE 5.0上的堆或内存映射区域中分配空间?
thanks...
ORIGINAL QUESTION
I am instantiating a FileStream Object (.NETCF , C#) using a file handle returned by native CreateFile() as below:
我正在使用本机CreateFile()返回的文件句柄实例化一个FileStream对象(.NETCF,C#),如下所示:
//P/Invoke
[DllImport("coredll.dll", SetLastError = true)]
public static extern IntPtr CreateFile(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
// File handle received from native Win32 API
IntPtr ptr= CreateFile("myfile.txt",
0,
0,
0,
FileMode.Create,
0,
IntPtr.Zero);
//Instantiate a FileStream object using handle (returned above) as parameter.
FileStream fs = new FileStream(ptr,FileAccess.ReadWrite);
The file will grow to large size > 500 KB or more. So, my questions are:
该文件将增长到大于500 KB或更大的大小。所以,我的问题是:
*1) Is there anything wrong with this way of doing things given that SafeFileHandle / Handle properties are not supported in .NETCF version ? Is there any better way of doing it (I am planning to use native memory mapped file handle with FileStream / MemoryStream) ?
* 1)由于.NETCF版本不支持SafeFileHandle / Handle属性,这种做事方式有什么问题吗?有没有更好的方法(我打算使用FileStream / MemoryStream本机内存映射文件句柄)?
2) Is the memory allocated by FileStream object fall under .NETCF garbage collector ? Or given that handle is of a memory mapped file created using native API, it (managed FileStream object and its resources) is out of purview of garbage collector ?*
2)FileStream对象分配的内存是否属于.NETCF垃圾收集器?或者假设句柄是使用本机API创建的内存映射文件,它(托管的FileStream对象及其资源)是否超出了垃圾收集器的范围?*
Thanks in advance.
提前致谢。
2 个解决方案
#1
Overall there is nothing wrong with this approach of using a native Create file and wrapping it in a FileStream object. This is a supported feature of FileStream.
总的来说,使用本机Create文件并将其包装在FileStream对象中的方法没有任何问题。这是FileStream支持的功能。
n terms of garbage collection though there are really 2 things at play here.
n垃圾收集的条款尽管这里有两件事情在玩。
- The memory associated with the FileStream object. Yes this will be garbage collected
- The handle which is a resource created with CreateFile. The FileStream object will take ownership of this handle and will free it when it is disposed (passively or actively).
与FileStream对象关联的内存。是的,这将是垃圾收集
句柄是使用CreateFile创建的资源。 FileStream对象将获得此句柄的所有权,并在处置(被动或主动)时释放它。
#2
According to the documentation, the constructor you're planning on using isn't available in .NET CF.
根据文档,您计划使用的构造函数在.NET CF中不可用。
#1
Overall there is nothing wrong with this approach of using a native Create file and wrapping it in a FileStream object. This is a supported feature of FileStream.
总的来说,使用本机Create文件并将其包装在FileStream对象中的方法没有任何问题。这是FileStream支持的功能。
n terms of garbage collection though there are really 2 things at play here.
n垃圾收集的条款尽管这里有两件事情在玩。
- The memory associated with the FileStream object. Yes this will be garbage collected
- The handle which is a resource created with CreateFile. The FileStream object will take ownership of this handle and will free it when it is disposed (passively or actively).
与FileStream对象关联的内存。是的,这将是垃圾收集
句柄是使用CreateFile创建的资源。 FileStream对象将获得此句柄的所有权,并在处置(被动或主动)时释放它。
#2
According to the documentation, the constructor you're planning on using isn't available in .NET CF.
根据文档,您计划使用的构造函数在.NET CF中不可用。