Example #1
0
 /// <summary>
 /// This method closes the connection to the device.
 /// </summary>
 public void Disconnect()
 {
     if (!IsConnected)
     {
         return;
     }
     ComDeviceObject.Close();
     IsConnected = false;
 }
Example #2
0
        /// <summary>
        /// This method opens a connection to the device.
        /// </summary>
        public void Connect()
        {
            if (IsConnected)
            {
                return;
            }

            var clientInfo = (IPortableDeviceValues) new PortableDeviceValuesClass();

            ComDeviceObject.Open(DeviceId, clientInfo);
            IsConnected = true;
        }
        internal void CopyToDevice(string source, string destination, bool overwrite)
        {
            IPortableDeviceContent content;

            ComDeviceObject.Content(out content);
            string parentObjectId = null;

            FindParentObjectId(Path.GetDirectoryName(destination), ref parentObjectId);
            if (string.IsNullOrEmpty(parentObjectId))
            {
                throw new Exception("destination folder has not been found.");
            }

            IPortableDeviceValues values =
                GetRequiredPropertiesForContentType(source, destination, parentObjectId);

            PortableDeviceApiLib.IStream wpdStream = null;
            uint optimalTransferSize = 0;

            content.CreateObjectWithPropertiesAndData(
                values,
                out wpdStream,
                ref optimalTransferSize,
                null);
            System.Runtime.InteropServices.ComTypes.IStream targetStream = null;
            try
            {
                targetStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;
                FileStream sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read);
                unsafe
                {
                    try
                    {
                        var buffer = new byte[optimalTransferSize];
                        int bytesRead;
                        do
                        {
                            bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSize);
                            IntPtr pcbWritten = IntPtr.Zero;
                            if (bytesRead < (int)optimalTransferSize)
                            {
                                targetStream.Write(buffer, bytesRead, pcbWritten);
                            }
                            else
                            {
                                targetStream.Write(buffer, (int)optimalTransferSize, pcbWritten);
                            }
                        } while (bytesRead > 0);
                        targetStream.Commit(0);
                    }
                    catch (ArgumentException e)
                    {
                        throw new Exception("Some argument has problem, remember this API can't overwrite" +
                                            "file on the device side! message: " + e.Message);
                    }
                    finally
                    {
                        sourceStream.Close();
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(wpdStream);
                Marshal.ReleaseComObject(targetStream);
            }
        }