简体   繁体   English

c# 中的 SafeFileHandle 是什么,我应该什么时候使用?

[英]What is SafeFileHandle in c# and when should i use?

While I am still learning System.IO, in File Stream class 's constructors, I found that there are overloaded constructors with the type named SafeFileHandle , I tried to search on the internet and the MSDN Documention, but I can't understand anything, and I found even stranger words, like IntPtr , can any one explain it to me? While I am still learning System.IO, in File Stream class 's constructors, I found that there are overloaded constructors with the type named SafeFileHandle , I tried to search on the internet and the MSDN Documention, but I can't understand anything,我发现了更奇怪的词,比如IntPtr ,谁能给我解释一下?

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

can someone explain it, or are there good websites that I can learn from..?有人可以解释一下吗,或者有没有好的网站可以学习..?

https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safefilehandle?view=netframework-4.8 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 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://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://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=2ahUKEwizlPG3ornlAhVFCKwKHUl9DxIQFjABegQIAxAB&url=https%3A%2Fen-us%2Fdocs.microsoft.com 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/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 https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safefilehandle?redirectedfrom=MSDN&view=netframework-4.8

These links provide info on SafeFileHandle , and some provide source code.这些链接提供有关SafeFileHandle的信息,有些提供源代码。

You can also check this out: How to Close SafeFile Handle properly您还可以查看: 如何正确关闭 SafeFile 句柄

IntPtr ... IntPtr ...

It's a "native (platform-specific) size integer."这是一个“本机(特定于平台)大小的 integer”。 It's internally represented as void* but exposed as an integer.它在内部表示为 void*,但公开为 integer。 You can use it whenever you need to store an unmanaged pointer and don't want to use unsafe code.您可以在需要存储非托管指针并且不想使用不安全代码时使用它。 IntPtr.Zero is effectively NULL (a null pointer). IntPtr.Zero 实际上是 NULL(一个 null 指针)。

Pointer ... Pointer ...

In general (across programming languages), a pointer is a number that represents a physical location in memory.通常(跨编程语言),指针是一个数字,表示 memory 中的物理位置。 A null pointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". null 指针(几乎总是)指向 0,并被广泛认为“不指向任何东西”。 Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system.由于系统支持的 memory 数量不同,因此保存该数字并不总是需要相同数量的字节,因此我们将其称为“本机大小整数”,它可以保存任何特定系统上的指针。

SafeFileHandle kernel32 ... 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);

More with SafeFileHandle and kernel32 ... SafeFileHandlekernel32的更多内容...

[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);

Though, if you are trying to open a File , then use the System.IO Controls但是,如果您尝试打开File ,请使用System.IO Controls

To simply open a file and read all of it's text:要简单地打开一个文件并阅读它的所有文本:

richTextBox1.Text = File.ReadAllText(yourfilename);

You can change the richTextBox1 to your Control 's name.您可以将richTextBox1更改为您的Control的名称。

I hope I am helping you, Prof Soft:)我希望我能帮助你,Soft 教授:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM