如果未指定流,File :: OpenRead会做什么? [英] What does File::OpenRead do if no stream is specified?

查看:130
本文介绍了如果未指定流,File :: OpenRead会做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一段代码,就像我想要的那样工作.但是我不确定到底会发生什么.
我有一个在主应用程序中使用的dll.该dll在运行时不应删除.因此,我可以在程序开始时打开一个流,并在程序结束时将其关闭:

Hi,

I''v got a piece of code which works like I want it to. But I''m not sure what exactly happens behind.
I''ve got a dll which I use in my main application. This dll should not be deletable during runtime. Therefore I can open a stream at the program start and close it at the end:

System::IO::FileStream^ fs = System::IO::File::OpenRead(fileName); //start
fs->Close(); //end



但是由于我在运行时不需要流(我只直接从dll访问某些.net类),因此可以这样做:



But since I don''t need the stream during runtime (I only access some .net classes from the dll directly) I can do it like this:

System::IO::File::OpenRead(fileName);



这也有效.该文件在运行时处于打开状态,无法删除.现在我的问题是:到底什么保持开放?没有定义流,但是无论如何都会打开文件.以及如何在没有程序流关闭的情况下在程序结束时将其关闭?

谢谢!
Mathias



This also works. The file is open during runtime and cannot be deleted. Now my question: What exactly stays opened? There is no stream defined but the file is opened anyways. And how can I close it at the program''s end without having a stream to close?

Thanks!
Mathias

推荐答案

该流已定义,您只是没有获取对该流的引用.
该流在其终结器运行时将关闭.

希望这会有所帮助,
Fredrik
The stream is defined you''re just not grabbing a reference to it.
The stream will close when its finalizer runs.

Hope this helps,
Fredrik


在操作系统级别对此进行了说明.您演示的打开操作实际上返回了文件句柄,该句柄可用于其他文件操作(也可以通过非OOP代码进行).可以通过独占访问或不进行独占访问来完成此操作,默认情况下为独占.每个进程都有其唯一的地址和句柄空间(如果您将相同的句柄作为相同的算术值传递给同一文件到另一个进程,则它将不能用作文件句柄),因此该句柄是该进程唯一的.当文件以独占访问方式打开时,该文件作为文件系统的对象被OS标记为忙;以这种方式,它不能被删除或以其他方式操作,直到最初抓住该句柄的进程要么显式关闭它,要么终止它.对进程(System.Diagnostics.Process.Kill的非正常终止也会释放该句柄.

—SA
This is explained at the OS level. Open operation you''ve demonstrated actually returns the file handle which can be used for other file operations (by non-OOP code as well). This operation can be done with exclusive access or not, exclusive being default. Each process has its unique address and handle space (if you pass the same handle to the same file to other process as identical arithmetic value it won''t work as a file handle), so the handle is unique to the process. When the file is opened with exclusive access, the file as the object of the file system is marked by OS as busy; in this way it cannot be deleted or operated otherwise until the process which originally grabbed the handle either explicitly close it or is terminated. Non-graceful killing of the process (System.Diagnostics.Process.Kill) also releases the handle.

—SA


这篇关于如果未指定流,File :: OpenRead会做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆