“文件编号"究竟是什么? [英] What exactly is a "File Number"?

查看:64
本文介绍了“文件编号"究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些早先从 VB6 转换过来的 C# 代码,它执行大量文件 I-O.我到处都看到这个:

I'm working in some C# code that had earlier been converted over from VB6 and it does lots of file I-O. All over the place I see this:

  fn = VBNET.FileSystem.FreeFile();

...接着是 VBNET.FileSystem.FileOpen(),一些文件 I-O,然后是 VBNET.FileSystem.FileClose().

...followed by VBNET.FileSystem.FileOpen(), some file I-O, and then VBNET.FileSystem.FileClose() .

调用 FreeFile() 会生成一个文件编号",这是打开文件所必需的.但是什么是文件编号?完成后如何将其释放回系统?

The call to FreeFile() generates a "file number", which is required to open the file. But what IS a file number and how do you release it back to the system when you're done with it?

http://msdn.microsoft 上的文档.com/en-us/library/microsoft.visualbasic.filesystem.freefile.aspx 似乎没有说,但它确实说如果超过 255 个文件正在使用",则会引发异常,这对我来说意味着在我处理完它们后释放它们是个好主意.

The documentation at http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.freefile.aspx doesn't seem to say, but it does say that an exception would be thrown if "more than 255 files are in use", which implies to me that it would be a good idea to release them when I'm done with them.

注意 - 我知道有更好的文件 IO 库可以使用,但在我们有资源重写这些东西之前,这是我们一直坚持的,所以我只想了解它.

N.B. - I understand that there are better file-IO libraries to use but this is what we're stuck with until we have resources to rewrite this stuff, so I just want to understand it.

推荐答案

那些 VB6 命令(FreeFile、FileOpen、FileClose、LOF 等)至少可以追溯到 QBasic.我希望文件号最初是一个 MS-DOS 文件句柄.

Those VB6 commands (FreeFile, FileOpen, FileClose, LOF, etc.) were present at least as far back as QBasic. I expect that the file number was originally an MS-DOS file handle.

一个快速的谷歌搜索出现了以下链接:

A quick google search came up with these links:

  • http://support.microsoft.com/kb/269030 - Maximum file handles in MS-DOS is 255, which may explain that limit. (Not really the subject of the page, but first link that I found.)
  • http://stanislavs.org/helppc/file_handles.html - The INT21 functions listed seem to match up with the old style file commands.

回到 QBasic(如果没记错的话),FileOpen 命令打开文件并保留文件句柄.FileClose 命令关闭文件并释放句柄.

Back in QBasic (if memory serves), the FileOpen command opened the file and reserved the file handle. The FileClose command closed the file and freed the handle.

FreeFile 只是一种获取未使用文件句柄的便捷方法:如果您知道您没有(例如)打开文件 #1,那么您只需调用 OPEN"C:\DOS\RUN" FOR INPUT AS #1 而不必费心调用 FreeFile.你仍然会用 CLOSE #1

FreeFile was just a convenience method to get an unused file handle: if you knew that you didn't have (for example) file #1 open, then you could just call OPEN "C:\DOS\RUN" FOR INPUT AS #1 and not bother calling FreeFile. You would still close it with CLOSE #1

我不记得在 VB6 中有没有改变.正如 Hans Passant 所提到的,在 .NET 内部,文件编号现在只是 VB6File 对象数组的索引.FileSystem.vb

I can't remember if that changed in VB6. As Hans Passant mentioned, internally in .NET the file number is now just an index into an array of VB6File objects. FileSystem.vb

这篇关于“文件编号"究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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