问题描述
虽然我仍在学习System.IO,但在File
Stream
类的构造函数中,我发现有名为SafeFileHandle
类型的重载构造函数,我尝试在Internet和MSDN Documention上进行搜索,但我什么都听不懂,我发现甚至 陌生人这样的词,例如IntPtr
,有人可以向我解释吗?
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?
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://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
这些链接提供有关SafeFileHandle
的信息,有些提供源代码.
These links provide info on SafeFileHandle
, and some provide source code.
您还可以查看以下内容:如何正确关闭SafeFile句柄
You can also check this out: How to Close SafeFile Handle properly
IntPtr
...
它是本地(特定于平台)的大小整数".它在内部表示为void *,但作为整数公开.只要需要存储非托管指针并且不想使用不安全的代码,就可以使用它. IntPtr.Zero实际上是NULL(空指针).
It's a "native (platform-specific) size integer." It's internally represented as void* but exposed as an 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).
Pointer
...
通常(跨编程语言),指针是代表内存中物理位置的数字.空指针(几乎总是)是一个指向0的指针,被广泛认为是不指向任何东西".由于系统具有不同数量的受支持内存,因此并不总是需要相同数量的字节来保存该数字,因此我们将本地大小整数"称为一个可以在任何特定系统上保存指针的字节.
In general (across programming languages), a pointer is a number that represents a physical location in memory. A null pointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". 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.
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);
但是,如果您尝试打开File
,请使用System.IO
Controls
Though, if you are trying to open a File
, then use the System.IO
Controls
要简单地打开文件并阅读所有文本,请执行以下操作:
To simply open a file and read all of it's text:
richTextBox1.Text = File.ReadAllText(yourfilename);
您可以将richTextBox1
更改为您的Control
名称.
You can change the richTextBox1
to your Control
's name.
我希望我能帮助您,Soft教授:)
I hope I am helping you, Prof Soft :)
这篇关于什么是C#中的SafeFileHandle?何时应使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!