Exemplo n.º 1
0
        private void FileTransferToGuest(VimService service, string vmKey, string username, string password, string fileName, byte[] fileData)
        {
            var auth = new NamePasswordAuthentication {
                username = username, password = password, interactiveSession = false
            };
            var vmRef = new ManagedObjectReference {
                type = "VirtualMachine", Value = vmKey
            };
            var fileMgr = new ManagedObjectReference {
                type = "GuestFileManager", Value = "guestOperationsFileManager"
            };
            var posixFileAttributes = new GuestPosixFileAttributes();

            posixFileAttributes.ownerId     = 1;
            posixFileAttributes.groupId     = 1;
            posixFileAttributes.permissions = (long)0777;             //execution file

            var requestUrl = service.InitiateFileTransferToGuest(fileMgr, vmRef, auth, fileName, posixFileAttributes, fileData.Length, true);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);

            request.ContentType   = "application/octet-stream";
            request.Method        = "PUT";
            request.ContentLength = fileData.Length;

            Stream requestStream = request.GetRequestStream();

            requestStream.Write(fileData, 0, fileData.Length);
            requestStream.Close();

            request.GetResponse();
        }