0
 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");
}

У меня выскакивает:

Предупреждение 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"

Можно ли это заменить?

Добавил текст

Я обычно импортировал:

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

Но если заменить его на:

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

То получаю ошибку в var stream = CreateFileW( что:

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

Решение проблемы

 [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);
1
  • 1
    var stream = CreateFileW( - проще было бы заменить на var stream = CreateFile(
    – user177221
    17 дек 2016 в 21:00

1 ответ 1

3

Прямо в сообщении об ошибке сказано, на что нужно заменить вызов - на перегрузку, которая принимает SafeFileHandle вместо IntPtr. Соответственно, вам нужно использовать другое объявление CreateFile:

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

Полный пример есть в MSDN по SafeFileHandle.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.