2

虽然我还在学习 System.IO,在File Streamclass 的构造函数中,我发现有名为 类型的重载构造函数SafeFileHandle,我尝试在互联网和 MSDN Documention 上搜索,但我什么都看不懂,我发现甚至更 陌生的词,比如IntPtr,谁能给我解释一下?

public FileStream (Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.IO.FileAccess access, int bufferSize, bool isAsync);

有人可以解释一下吗,或者有没有好的网站可以学习..?

4

1 回答 1

6

https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safefilehandle?view=netframework-4.8

http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/clr/src/BCL/Microsoft/Win32/SafeHandles/SafeFileHandle@cs/1/SafeFileHandle@ CS

https://csharp.hotexamples.com/examples/-/SafeFileHandle/-/php-safefilehandle-class-examples.html

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=2ahUKEwizlPG3ornlAhVFCKwKHUl9DxIQFjABegQIAxAB&url=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet% 2Fapi%2Fmicrosoft.win32.safehandles.safefilehandle.-ctor&usg=AOvVaw3M0YPCVH1439KghalbcDfG

https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream.safefilehandle?view=netframework-4.8

https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safefilehandle?redirectedfrom=MSDN&view=netframework-4.8

这些链接提供有关 的信息SafeFileHandle,有些提供源代码。

您还可以查看:如何正确关闭 SafeFile 句柄

IntPtr...

这是一个“本机(特定于平台)大小的整数”。它在内部表示为 void* 但公开为整数。您可以在需要存储非托管指针并且不想使用不安全代码时使用它。IntPtr.Zero 实际上是 NULL(空指针)。

Pointer...

通常(跨编程语言),指针是表示内存中物理位置的数字。空指针(几乎总是)指向 0,并且被广泛认为是“不指向任何东西”。由于系统支持的内存量不同,因此保存该数字并不总是需要相同数量的字节,因此我们称其为“本机大小整数”,它可以保存任何特定系统上的指针。

SafeFileHandle kernel32...

[DllImport("kernel32.dll", SetLastError = true, CharSet=CharSet.Unicode)]
static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess,
  uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
  uint dwFlagsAndAttributes, IntPtr hTemplateFile);

更多... SafeFileHandle_kernel32

[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
private SafeFileHandle handleValue = null;
handleValue = CreateFile(
Path,
GENERIC_WRITE,
0,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);

但是,如果您尝试打开 a File,则使用System.IO Controls

要简单地打开一个文件并阅读它的所有文本:

richTextBox1.Text = File.ReadAllText(yourfilename);

您可以将 更改richTextBox1为您Control的姓名。

我希望我能帮助你,Soft 教授 :)

于 2019-10-26T05:55:16.053 回答