-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteDisk.cs
More file actions
29 lines (26 loc) · 896 Bytes
/
WriteDisk.cs
File metadata and controls
29 lines (26 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.IO;
public static class WriteDisk
{
public static void Write(string driveLetter, int sectorNumber, byte[] data)
{
int sectorSize = 512; // Standardgröße eines Sektors
if (data.Length != sectorSize)
{
throw new ArgumentException($"Data must be exactly {sectorSize} bytes long.");
}
string drivePath = $@"\\.\{driveLetter}:";
try
{
using (FileStream fs = new FileStream(drivePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
fs.Seek(sectorNumber * sectorSize, SeekOrigin.Begin);
fs.Write(data, 0, sectorSize);
}
} catch (Exception ex)
{
// System error message popup
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}