Renci.SshNet.Sftp.SftpFileAttributes.GetBytes C# (CSharp) Method

GetBytes() public method

Returns a byte array representing the current SftpFileAttributes.
public GetBytes ( ) : byte[]
return byte[]
        public byte[] GetBytes()
        {
            var stream = new SshDataStream(4);

            uint flag = 0;

            if (IsSizeChanged && IsRegularFile)
            {
                flag |= 0x00000001;
            }

            if (IsUserIdChanged || IsGroupIdChanged)
            {
                flag |= 0x00000002;
            }

            if (IsPermissionsChanged)
            {
                flag |= 0x00000004;
            }

            if (IsLastAccessTimeChanged || IsLastWriteTimeChanged)
            {
                flag |= 0x00000008;
            }

            if (IsExtensionsChanged)
            {
                flag |= 0x80000000;
            }

            stream.Write(flag);

            if (IsSizeChanged && IsRegularFile)
            {
                stream.Write((ulong) Size);
            }

            if (IsUserIdChanged || IsGroupIdChanged)
            {
                stream.Write((uint) UserId);
                stream.Write((uint) GroupId);
            }

            if (IsPermissionsChanged)
            {
                stream.Write(Permissions);
            }

            if (IsLastAccessTimeChanged || IsLastWriteTimeChanged)
            {
                var time = (uint)(LastAccessTime.ToFileTime() / 10000000 - 11644473600);
                stream.Write(time);
                time = (uint)(LastWriteTime.ToFileTime() / 10000000 - 11644473600);
                stream.Write(time);
            }

            if (IsExtensionsChanged)
            {
                foreach (var item in Extensions)
                {
                    // TODO: we write as ASCII but read as UTF8 !!!

                    stream.Write(item.Key, SshData.Ascii);
                    stream.Write(item.Value, SshData.Ascii);
                }
            }

            return stream.ToArray();
        }

Usage Example

        public void Init()
        {
            var random = new Random();

            _protocolVersion = (uint)random.Next(0, int.MaxValue);
            _requestId = (uint)random.Next(0, int.MaxValue);
            _handle = new byte[random.Next(1, 10)];
            random.NextBytes(_handle);
            _attributes = SftpFileAttributes.Empty;
            _attributesBytes = _attributes.GetBytes();
        }
All Usage Examples Of Renci.SshNet.Sftp.SftpFileAttributes::GetBytes