2

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..?

1 Answer 1

7

https://learn.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%2Flearn.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.win32.safehandles.safefilehandle.-ctor&usg=AOvVaw3M0YPCVH1439KghalbcDfG

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

https://learn.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.

You can also check this out: How to Close SafeFile Handle properly

IntPtr...

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...

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

More with SafeFileHandle and 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);

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

You can change the richTextBox1 to your Control's name.

I hope I am helping you, Prof Soft :)

11
  • 1
    ohh thanks broo , but i already opened this links when i was searching and i cant understand , if you know what is safe file handle IntPtr can you explain it to me by your style probably i understand from you broo :)
    – Salo7ty
    Commented Oct 26, 2019 at 6:10
  • I updated my answer with more links :) I don't have as much experience with SafeFileHandle
    – Momoro
    Commented Oct 26, 2019 at 6:15
  • ohh I do not know what to say thank you for caring , i am waiting you :)
    – Salo7ty
    Commented Oct 26, 2019 at 6:16
  • Once again, I updated my answer(With as much detail as I could do at the moment)
    – Momoro
    Commented Oct 26, 2019 at 6:22
  • If you need more info, just ask :)
    – Momoro
    Commented Oct 26, 2019 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.