我有一个简单的函数,可以从 C:\ 驱动器中获取硬盘序列号并将其放入字符串中:
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"C:\"");
disk.Get();
string hdStr = Convert.ToString(disk["VolumeSerialNumber"]);
然后,我尝试将上面的字符串转换为 ASCII,然后将其写入二进制文件,我遇到的问题是,在转换此字符串并使用 streamwriter 保存文件并在十六进制编辑器中打开文件时,我看到了更多原本想写的字节,例如“16342D1F4A61BC”
将出现:08 16 34 2d 1f 4a 61 c2 bc
它以某种方式在其中添加了 08 和 c2 ......
更完整的版本如下:
string constructor2 = "16342D1F4A61BC";
string StrValue = "";
while (constructor2.Length > 0)
{
StrValue += System.Convert.ToChar(System.Convert.ToUInt32(constructor2.Substring(0, 2), 16)).ToString();
// Remove from the hex object the converted value
constructor2 = constructor2.Substring(2, constructor2.Length - 2);
}
FileStream writeStream;
try
{
writeStream = new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + "\\license.mgr", FileMode.Create);
BinaryWriter writeBinay = new BinaryWriter(writeStream);
writeBinay.Write(StrValue);
writeBinay.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
谁能帮我了解这些是如何添加的?