C# 我想改变分配的字节数

C# I want to change the bytes allocated

            Byte[] mtime = new Byte[4];
            mtime = BitConverter.GetBytes(root.LastWriteTime.ToBinary());
            fs.Write(mtime, 0, mtime.Length);

enter image description here

保存二进制文件时。它(LastWriteTime)占用8Bytes。 我希望 'LastWriteTime' 只使用 4Bytes。 我应该怎么办? 有办法吗?

对不起,我英语不好....

您不能在不丢失信息(即精度)的情况下缩小 DateTime 对象的表示形式。如果您可以接受丢失信息,则只需确定哪些信息对您最重要。正如 this answer 所示,您可以从基础数据类型(long)中截断信息,并只获取您需要的信息。

If you need millisecond precision only, why not lose the least significant bits

int timeStamp = (int)(DateTime.Now.Ticks >> 10) // lose smallest 10 bits