[C#] Direkt Zugriff auf Datenträger

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von AliveDevil.

    [C#] Direkt Zugriff auf Datenträger

    Hi,

    ich habe ein "kleines" Problem mit einer 1zu1 Kopie von einer DVD zu einem USB-Stick.
    Was ich konkret machen will:
    DVD byteweise auslesen und auf den USB-Stick kopieren.
    Ich habe es mit
    dieser API

    Quellcode

    1. using System;
    2. using System.Runtime.InteropServices;
    3. using System.IO;
    4. using Microsoft.Win32.SafeHandles;
    5. namespace ReadFromDevice
    6. {
    7. public class DeviceStream : Stream, IDisposable
    8. {
    9. public const short FILE_ATTRIBUTE_NORMAL = 0x80;
    10. public const short INVALID_HANDLE_VALUE = -1;
    11. public const uint GENERIC_READ = 0x80000000;
    12. public const uint GENERIC_WRITE = 0x40000000;
    13. public const uint CREATE_NEW = 1;
    14. public const uint CREATE_ALWAYS = 2;
    15. public const uint OPEN_EXISTING = 3;
    16. // Use interop to call the CreateFile function.
    17. // For more information about CreateFile,
    18. // see the unmanaged MSDN reference library.
    19. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    20. private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
    21. uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
    22. uint dwFlagsAndAttributes, IntPtr hTemplateFile);
    23. [DllImport("kernel32.dll", SetLastError = true)]
    24. private static extern bool ReadFile(
    25. IntPtr hFile, // handle to file
    26. byte[] lpBuffer, // data buffer
    27. int nNumberOfBytesToRead, // number of bytes to read
    28. ref int lpNumberOfBytesRead, // number of bytes read
    29. IntPtr lpOverlapped
    30. //
    31. // ref OVERLAPPED lpOverlapped // overlapped buffer
    32. );
    33. private SafeFileHandle handleValue = null;
    34. private FileStream _fs = null;
    35. public DeviceStream(string device)
    36. {
    37. Load(device);
    38. }
    39. private void Load(string Path)
    40. {
    41. if (string.IsNullOrEmpty(Path))
    42. {
    43. throw new ArgumentNullException("Path");
    44. }
    45. // Try to open the file.
    46. IntPtr ptr = CreateFile(Path, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
    47. handleValue = new SafeFileHandle(ptr, true);
    48. _fs = new FileStream(handleValue, FileAccess.Read);
    49. // If the handle is invalid,
    50. // get the last Win32 error
    51. // and throw a Win32Exception.
    52. if (handleValue.IsInvalid)
    53. {
    54. Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    55. }
    56. }
    57. public override bool CanRead
    58. {
    59. get { return true; }
    60. }
    61. public override bool CanSeek
    62. {
    63. get { return false; }
    64. }
    65. public override bool CanWrite
    66. {
    67. get { return false; }
    68. }
    69. public override void Flush()
    70. {
    71. return;
    72. }
    73. public override long Length
    74. {
    75. get { return -1; }
    76. }
    77. public override long Position
    78. {
    79. get
    80. {
    81. throw new NotImplementedException();
    82. }
    83. set
    84. {
    85. throw new NotImplementedException();
    86. }
    87. }
    88. /// <summary>
    89. /// </summary>
    90. /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and
    91. /// (offset + count - 1) replaced by the bytes read from the current source. </param>
    92. /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream. </param>
    93. /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
    94. /// <returns></returns>
    95. public override int Read(byte[] buffer, int offset, int count)
    96. {
    97. int BytesRead = 0;
    98. var BufBytes = new byte[count];
    99. if (!ReadFile(handleValue.DangerousGetHandle(), BufBytes, count, ref BytesRead, IntPtr.Zero))
    100. {
    101. Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    102. }
    103. for (int i = 0; i < BytesRead; i++)
    104. {
    105. buffer[offset + i] = BufBytes[i];
    106. }
    107. return BytesRead;
    108. }
    109. public override int ReadByte()
    110. {
    111. int BytesRead = 0;
    112. var lpBuffer = new byte[1];
    113. if (!ReadFile(
    114. handleValue.DangerousGetHandle(), // handle to file
    115. lpBuffer, // data buffer
    116. 1, // number of bytes to read
    117. ref BytesRead, // number of bytes read
    118. IntPtr.Zero
    119. ))
    120. { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); ;}
    121. return lpBuffer[0];
    122. }
    123. public override long Seek(long offset, SeekOrigin origin)
    124. {
    125. throw new NotImplementedException();
    126. }
    127. public override void SetLength(long value)
    128. {
    129. throw new NotImplementedException();
    130. }
    131. public override void Write(byte[] buffer, int offset, int count)
    132. {
    133. throw new NotImplementedException();
    134. }
    135. public override void Close()
    136. {
    137. handleValue.Close();
    138. handleValue.Dispose();
    139. handleValue = null;
    140. base.Close();
    141. }
    142. private bool disposed = false;
    143. new void Dispose()
    144. {
    145. Dispose(true);
    146. base.Dispose();
    147. GC.SuppressFinalize(this);
    148. }
    149. private new void Dispose(bool disposing)
    150. {
    151. // Check to see if Dispose has already been called.
    152. if (!this.disposed)
    153. {
    154. if (disposing)
    155. {
    156. if (handleValue != null)
    157. {
    158. _fs.Dispose();
    159. handleValue.Close();
    160. handleValue.Dispose();
    161. handleValue = null;
    162. }
    163. }
    164. // Note disposing has been done.
    165. disposed = true;
    166. }
    167. }
    168. }
    169. }

    versucht.
    das Problem dabei ist, dass ich

    Quellcode

    1. DeviceStream test = new DeviceStream(@"\\.\CdRom0");
    2. DeviceStream test = new DeviceStream(@"\\.\CdRom0\");

    über den ersten Code Zugriff drauf erhalte, aber ich nichts darauf machen kann.
    der zweite Code sollte eigentlich Zugriff auf das ganze Dateisystem geben, aber die einzige Fehlermeldung die ich bekomme ist

    Quellcode

    1. Ungültiges Handle.
    2. Parametername: handle

    Hoffe jemand kann mir helfen.

    und ja, ich möchte das CDFS auf dem Stick haben.