private static void WriteAlternateStream(string _path, string _text) { var stream = CreateFileW(_path, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero); using (FileStream fs = new FileStream(stream,FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(_text); sw.Close(); } } } public static void Id(string _PathExe) { var x = _PathExe + ":Zone.Identifier"; WriteAlternateStream(x, "[ZoneTransfer]\r\nZoneId=1"); } 

I pop up:

Предупреждение 1 "System.IO.FileStream.FileStream(System.IntPtr, System.IO.FileAccess)" является устаревшим: "This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202"

Can this be replaced?

Added text

I usually imported:

 [DllImport("kernel32.dll", EntryPoint = "CreateFileW")] public static extern System.IntPtr CreateFileW( [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] string lpFileName, uint dwDesiredAccess,uint dwShareMode, [InAttribute()] IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, [InAttribute()] IntPtr hTemplateFile); 

But if you replace it with:

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

Then I get an error in var stream = CreateFileW( that:

Элемент "CreateFileW" не существует в текущем контексте.

Solution to the problem

  [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "CreateFileW", CharSet = CharSet.Unicode)] static extern SafeFileHandle CreateFileW(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); 
  • one
    var stream = CreateFileW( - it would be easier to replace with var stream = CreateFile( - PashaPash

1 answer 1

Right in the error message it says, what needs to be replaced with a call - with an overload, which accepts SafeFileHandle instead of IntPtr . Accordingly, you need to use another CreateFile declaration:

 // Use interop to call the CreateFile function. // For more information about CreateFile, // see the unmanaged MSDN reference library. [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); 

A full example is in MSDN for SafeFileHandle .