Из того, что я вижу в том контексте, Вы не должны копировать SomeByteArray
в буфер. Просто необходимо получить дескриптор от SomeByteArray
, прикрепить его, скопировать IntPtr
данные с помощью PtrToStructure
и затем выпустить. Никакая потребность в копии.
, Который был бы:
NewStuff ByteArrayToNewStuff(byte[] bytes)
{
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
NewStuff stuff = (NewStuff)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(NewStuff));
}
finally
{
handle.Free();
}
return stuff;
}
Универсальная версия:
T ByteArrayToStructure<T>(byte[] bytes) where T: struct
{
T stuff;
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
}
finally
{
handle.Free();
}
return stuff;
}
Более простая версия (требует unsafe
переключатель):
unsafe T ByteArrayToStructure<T>(byte[] bytes) where T : struct
{
fixed (byte* ptr = &bytes[0])
{
return (T)Marshal.PtrToStructure((IntPtr)ptr, typeof(T));
}
}
Пожалуйста, поймите, что Windows Vista и Windows 7 используют виртуализацию для защиты таких папок, поэтому вам необходимо проверить, находится ли myDir в виртуальном хранилище,
C: \ Users (имя пользователя) \ AppData \ Local \ VirtualStore \ ProgramData
CommonApplicationData
обычно преобразуется в
в Windows 7. Это скрытая папка. Если вы не просите проводник отображать скрытые файлы и папки (из интерфейса Параметры папки -> Просмотр ), вы не увидите это в проводнике.
ИЗМЕНИТЬ : Убедитесь, что вы просматриваете правильный каталог в проводнике: перейдите к % PROGRAMDATA% , а не к C: \ ProgramData.
I don't know why Windows 7 is doing that, and I don't have a copy to test, but your check to Directory.Exists(path) shouldn't be necessary. If you reflect (deep) into Directory.CreateDirectory(path), you'll find that it internally checks to see if the directory already exists, and it's not a problem to call it multiple times on a directory that already exists. The call to Directory.Exists(path) is extraneous and unnecessary.
Of course, if Windows 7 isn't doing the Directory.Exists the way I'd expect, maybe it doesn't do the naked Directory.CreateDirectory either. In any case, it's worth testing.