private static void StringToPropVariant(
            string value,
            out PortableDeviceApiLib.tag_inner_PROPVARIANT propvarValue, uint pid)
        {
            PortableDeviceApiLib.IPortableDeviceValues pValues =
                (PortableDeviceApiLib.IPortableDeviceValues)
                new PortableDeviceTypesLib.PortableDeviceValuesClass();

            var WPD_OBJECT_ID = new _tagpropertykey();

            WPD_OBJECT_ID.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA,
                         0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_ID.pid = pid;



            pValues.SetStringValue(ref WPD_OBJECT_ID, value);

            pValues.GetValue(ref WPD_OBJECT_ID, out propvarValue);
        }
        public void CopyFilefromDevice(PortableDeviceFile file, string saveToPath)
        {
            unsafe
            {
                IPortableDeviceContent       portableDeviceContent;
                IPortableDeviceResources     portableDeviceResource;
                PortableDeviceApiLib.IStream stream;
                int num = 0;

                _device.Content(out portableDeviceContent);
                portableDeviceContent.Transfer(out portableDeviceResource);

                uint num1 = 0;
                PortableDeviceApiLib._tagpropertykey __tagpropertykey = new PortableDeviceApiLib._tagpropertykey()
                {
                    fmtid = new Guid(-400655938, 13552, 16831, 181, 63, 241, 160, 106, 232, 120, 66),
                    pid   = 0
                };

                portableDeviceResource.GetStream(file.Id, ref __tagpropertykey, 0, ref num1, out stream);
                System.Runtime.InteropServices.ComTypes.IStream stream1 = (System.Runtime.InteropServices.ComTypes.IStream)stream;

                string str = (string.IsNullOrEmpty(file.Name) ? Path.GetFileName(file.Id) : Path.GetFileName(file.Name));

                var fileStream = new FileStream(Path.Combine(saveToPath, str), FileMode.Create, FileAccess.Write);

                byte[] numArray = new byte[1024];
                do
                {
                    //stream1.Read(numArray, 1024, new IntPtr(ref num));
                    stream1.Read(numArray, 1024, new IntPtr(&num));
                    fileStream.Write(numArray, 0, 1024);
                }while (num > 0);

                fileStream.Close();

                Marshal.ReleaseComObject(stream1);
                Marshal.ReleaseComObject(stream);
            }
        }
Example #3
0
        public void DownloadFile(PortableDeviceFile file, string saveToPath)
        {
            IPortableDeviceContent content;

            this._device.Content(out content);

            IPortableDeviceResources resources;

            content.Transfer(out resources);

            PortableDeviceApiLib.IStream wpdStream;
            uint optimalTransferSize = 0;

            var property = new _tagpropertykey();

            property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
            property.pid   = 0;

            resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

            System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

            var        filename     = Path.GetFileName(file.Name);
            FileStream targetStream = new FileStream(Path.Combine(saveToPath, filename), FileMode.Create, FileAccess.Write);

            unsafe
            {
                var buffer = new byte[1024];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);
                targetStream.Close();
            }

            Marshal.ReleaseComObject(sourceStream);
            Marshal.ReleaseComObject(wpdStream);
        }
Example #4
0
        private IPortableDeviceValues GetRequiredPropertiesForContentType(string fileName, string parentObjectId)
        {
            var values = new PortableDeviceValues() as IPortableDeviceValues;

            var wpdObjectParentId = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 3
            };

            values.SetStringValue(ref wpdObjectParentId, parentObjectId);

            var fileInfo = new FileInfo(fileName);

            var wpdObjectSize = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 11
            };

            values.SetUnsignedLargeIntegerValue(wpdObjectSize, (ulong)fileInfo.Length);

            var wpdObjectOriginalFileName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 12
            };

            values.SetStringValue(wpdObjectOriginalFileName, Path.GetFileName(fileName));

            var wpdObjectName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 4
            };

            values.SetStringValue(wpdObjectName, Path.GetFileName(fileName));

            return(values);
        }
Example #5
0
        private static PortableDeviceObject WrapObject(IPortableDeviceProperties properties, string objectId)
        {
            IPortableDeviceKeyCollection keys;

            properties.GetSupportedProperties(objectId, out keys);

            IPortableDeviceValues values;

            properties.GetValues(objectId, keys, out values);

            // Get the name of the object
            string name;
            var    property = new _tagpropertykey();

            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 4;
            values.GetStringValue(property, out name);

            // Get the type of the object
            Guid contentType;

            property       = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 7;
            values.GetGuidValue(property, out contentType);

            var folderType = new Guid(0x27E2E392, 0xA111, 0x48E0, 0xAB, 0x0C,
                                      0xE1, 0x77, 0x05, 0xA0, 0x5F, 0x85);
            var functionalType = new Guid(0x99ED0160, 0x17FF, 0x4C44, 0x9D, 0x98,
                                          0x1D, 0x7A, 0x6F, 0x94, 0x19, 0x21);

            if (contentType == folderType || contentType == functionalType)
            {
                return(new PortableDeviceFolder(objectId, name));
            }

            return(new PortableDeviceFile(objectId, name));
        }
Example #6
0
        public PropVariant[] GetPropVariants(PropertyKey key)
        {
            if (!this.result.TryGetIUnknownValue(key, out object obj))
            {
                return(new PropVariant[0]);
            }
            var col = obj as IPortableDevicePropVariantCollection;

            uint count = 0;

            col.GetCount(ref count);
            var result = new PropVariant[count];

            for (uint i = 0; i < count; i++)
            {
                PROPVARIANT val = new PROPVARIANT();
                col.GetAt(i, ref val);
                result[i] = PropVariant.FromValue(val);
            }

            return(result);
        }
Example #7
0
        private IPortableDeviceValues GetRequiredPropertiesForFolder(string folderName, string parentObjectId)
        {
            var values = new PortableDeviceValues() as IPortableDeviceValues;

            var wpdObjectContentType = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 7
            };

            values.SetGuidValue(ref wpdObjectContentType, CreateContentTypeFolderGuid());

            var wpdObjectParentId = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 3
            };

            values.SetStringValue(ref wpdObjectParentId, parentObjectId);

            var wpdObjectOriginalFileName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 12
            };

            values.SetStringValue(wpdObjectOriginalFileName, Path.GetFileName(folderName));

            var wpdObjectName = new _tagpropertykey
            {
                fmtid = CreateFmtidGuid(),
                pid   = 4
            };

            values.SetStringValue(wpdObjectName, Path.GetFileName(folderName));

            return(values);
        }
Example #8
0
        public static void WriteObject(IPortableDeviceValues values)
        {
            uint num = 0;

            values.GetCount(ref num);
            for (uint i = 0; i < num; i++)
            {
                PropertyKey key = new PropertyKey();
                PROPVARIANT val = new PROPVARIANT();
                values.GetAt(i, ref key, ref val);

                Type twpd   = typeof(WPD);
                var  fields = twpd.GetFields().Where(f => f.FieldType == typeof(PropertyKey)).ToList();
                foreach (var field in fields)
                {
                    PropertyKey pk = (PropertyKey)field.GetValue(null);
                    if (pk.pid == key.pid && pk.fmtid == key.fmtid)
                    {
                        Trace.WriteLine($"##### {field.Name} = {((PropVariant)val).ToString()}");
                    }
                }
            }
        }
        public void DownloadFile(PortableDeviceFile file, string saveToPath)
        {
            IPortableDeviceContent content;
            this._device.Content(out content);

            IPortableDeviceResources resources;
            content.Transfer(out resources);

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

            var property = new _tagpropertykey();
            property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);

            property.pid = 0;
            bool done = false;
            int numTries = 3;

            while(!done)
            {
                try
                {
                    resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);
                    done = true;
                }
                catch (System.Runtime.InteropServices.COMException err)
                {
                    Console.WriteLine("-----------exception----------");
                    Console.WriteLine(err.GetType());
                    Console.WriteLine(err.Message);
                    Console.WriteLine(err.Source);
                    Console.WriteLine(err.TargetSite);
                    Console.WriteLine(err.ErrorCode);
                    Console.WriteLine(err.HelpLink);
                    Console.WriteLine("------------------------------");
                    System.Threading.Thread.Sleep(5000); //this codes make your application waiting for 5 seconds
                    numTries--;
                    resources.Cancel();
                    if (numTries < 0)
                        return;
                }
            }

            System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream) wpdStream;

            //var filename = Path.GetFileName(file.Id);
            var filename = Path.GetFileName(file.Name);
            FileStream targetStream = new FileStream(Path.Combine(saveToPath, filename), FileMode.Create, FileAccess.Write);

            unsafe
            {
                int size = 1024;
                var buffer = new byte[size];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, size, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, size);
                } while (bytesRead > 0);
                targetStream.Close();
                Marshal.ReleaseComObject(sourceStream);
                Marshal.ReleaseComObject(wpdStream);
            }
        }
Example #10
0
        private static PortableDeviceObject WrapObject(IPortableDeviceProperties properties,
            string objectId)
        {
            IPortableDeviceKeyCollection keys;
            properties.GetSupportedProperties(objectId, out keys);

            IPortableDeviceValues values;
            properties.GetValues(objectId, keys, out values);


            uint count = 0;
            keys.GetCount(ref count);
            for (uint i = 0; i < count; i++)
            {
                var property_info = new PortableDeviceApiLib._tagpropertykey();
                keys.GetAt(i, ref property_info);
                Console.WriteLine(property_info.fmtid.ToString() + "    " + property_info.pid.ToString());
            }
            //Console.ReadLine();
            //return;
            // Get the name of the object
            string name;
            var property = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 4;
            try
            {
                values.GetStringValue(property, out name);
            }
            catch (Exception ex)
            {
                try
                {
                    property.pid = 12;
                    values.GetStringValue(property, out name);
                }
                catch
                {
                    name = "ERROR-NOT-FOUND";
                }
            }
            // Get the type of the object
            Guid contentType;
            property = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 7;
            values.GetGuidValue(property, out contentType);

            var folderType = new Guid(0x27E2E392, 0xA111, 0x48E0, 0xAB, 0x0C,
                                      0xE1, 0x77, 0x05, 0xA0, 0x5F, 0x85);
            var functionalType = new Guid(0x99ED0160, 0x17FF, 0x4C44, 0x9D, 0x98,
                                          0x1D, 0x7A, 0x6F, 0x94, 0x19, 0x21);

            if (contentType == folderType || contentType == functionalType)
            {
                return new PortableDeviceFolder(objectId, name);
            }

            return new PortableDeviceFile(objectId, name);
        }
        public bool DownloadFile(PortableDeviceFile file, string saveToPath)
        {
            bool isdownload = false;
            IPortableDeviceContent content;

            this._device.Content(out content);

            IPortableDeviceResources resources;

            content.Transfer(out resources);

            PortableDeviceApiLib.IStream wpdStream;
            uint optimalTransferSize = 0;

            System.Runtime.InteropServices.ComTypes.IStream sourceStream = null;

            IPortableDeviceValues values =
                new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

            DateTime  createdDate = GetObjectCreationTime(content, file.Id);
            ImageItem imageItem   = null;

            imageItem = new ImageItem(0, file.Id, file.Name, createdDate);


            var property = new _tagpropertykey();

            property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
            property.pid   = 0;
            string     imgpath      = string.Empty;
            FileStream targetStream = null;

            try
            {
                resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

                sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

                var filename = Path.GetFileName(file.Id);
                imgpath      = saveToPath + "IMG_1_" + imageItem.TakenTime.ToString() + "_" + file.Id + ".jpg";
                targetStream = new FileStream(imgpath, FileMode.Create, FileAccess.Write);

                unsafe
                {
                    var buffer = new byte[2048];
                    int bytesRead;
                    do
                    {
                        sourceStream.Read(buffer, 2048, new IntPtr(&bytesRead));
                        targetStream.Write(buffer, 0, 2048);
                    } while (bytesRead > 0);
                    targetStream.Close();


                    isdownload = true;
                }
            }
            catch (Exception ex)
            {
                isdownload = false;
                Logger.Basic.Error("PortableDevice DownloadFile : " + ex.ToString(), ex);
            }
            finally
            {
                if (resources != null && Marshal.IsComObject(resources))
                {
                    Marshal.ReleaseComObject(resources);
                }

                if (content != null && Marshal.IsComObject(content))
                {
                    Marshal.ReleaseComObject(content);
                }

                if (sourceStream != null && Marshal.IsComObject(sourceStream))
                {
                    Marshal.ReleaseComObject(sourceStream);
                }

                if (!isdownload)
                {
                    if (File.Exists(imgpath))
                    {
                        if (targetStream != null)
                        {
                            targetStream.Close();
                        }

                        File.Delete(imgpath);
                    }
                }
            }

            return(isdownload);
        }
Example #12
0
 public static Command Create(PropertyKey commandKey)
 {
     return(new Command(commandKey));
 }
        /// <summary>
        /// lookup the given key and return the date property associated with the object
        /// </summary>
        /// <param name="deviceContent">unmanged device</param>
        /// <param name="objectId">object id</param>
        /// <param name="key">name of the property key</param>
        /// <returns>date value</returns>
        public DateTime GetDateProperty(IPortableDeviceContent deviceContent, string objectId, _tagpropertykey key)
        {
            var deviceValues = GetDeviceValues(deviceContent, key, objectId);

            PortableDeviceApiLib.tag_inner_PROPVARIANT value;
            deviceValues.GetValue(ref key, out value);

            return(PropVariant.FromValue(value).ToDate());
        }
 public void SetGuidProperty(string objectId, _tagpropertykey propertyKey, Guid value)
 {
     SetProperty(objectId, propertyKey, values => values.SetGuidValue(propertyKey, value));
 }
        /// <summary>
        ///     Extract command supported by device
        /// </summary>
        /// <param name="portableDeviceClass"></param>
        internal void ExtractCommands(PortableDeviceClass portableDeviceClass)
        {
            IPortableDeviceCapabilities capabilities;
            portableDeviceClass.Capabilities(out capabilities);

            IPortableDeviceKeyCollection values;
            capabilities.GetSupportedCommands(out values);

            var key = new _tagpropertykey();
            _tagpropertykey tt;

            uint count = 1;
            values.GetCount(ref count);
            for (uint i = 0; i < count; i++)
            {
                values.GetAt(i, ref key);

                string currentName = null;
                foreach (FieldInfo fi in typeof (PortableDevicePKeys).GetFields())
                {
                    tt = (_tagpropertykey) fi.GetValue(null);
                    if (key.fmtid == tt.fmtid && key.pid == tt.pid)
                        currentName = fi.Name;
                }

                commands.Add(currentName ?? key.pid + " " + key.fmtid, key);
            }
        }
        private static PortableDeviceObject WrapObject(IPortableDeviceProperties properties,
            string objectId)
        {
            IPortableDeviceKeyCollection keys;
            properties.GetSupportedProperties(objectId, out keys);

            IPortableDeviceValues values;
            properties.GetValues(objectId, keys, out values);

            // Get the name of the object
            string name;
            string date;
            string size;
            var property = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 4;
            values.GetStringValue(property, out name);

            // Get the type of the object
            Guid contentType;
            property = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 7;
            values.GetGuidValue(property, out contentType);

            var folderType = new Guid(0x27E2E392, 0xA111, 0x48E0, 0xAB, 0x0C,
                                      0xE1, 0x77, 0x05, 0xA0, 0x5F, 0x85);
            var functionalType = new Guid(0x99ED0160, 0x17FF, 0x4C44, 0x9D, 0x98,
                                          0x1D, 0x7A, 0x6F, 0x94, 0x19, 0x21);

            if (contentType == folderType || contentType == functionalType)
            {
                return new PortableDeviceFolder(objectId, name, "", "", 0);
            }

            property.pid = 12;//WPD_OBJECT_ORIGINAL_FILE_NAME
            values.GetStringValue(property, out name);
            try
            {
                property.pid = 19;//WPD_OBJECT_DATE_MODIFIED
                values.GetStringValue(property, out date);
            }
            catch (Exception ex)
            {
                property.pid = 18;//WPD_OBJECT_DATE_MODIFIED
                values.GetStringValue(property, out date);
            }
            property.pid = 11;//WPD_OBJECT_SIZE
            values.GetStringValue(property, out size);
            return new PortableDeviceFile(objectId, name, date, size, Int32.Parse(size));
        }
Example #17
0
 public void Add(PropertyKey key, int value)
 {
     this.values.SetSignedIntegerValue(key, value);
 }
Example #18
0
        // COPY inside of the same WPD
        public bool CopyInsideWPD2(PortableDeviceFile file, string parentObjectId)
        {
            bool success = false;

            // WPD source
            IPortableDeviceContent contentSrc;

            this._device.Content(out contentSrc);
            IPortableDeviceResources resourcesSrc;

            contentSrc.Transfer(out resourcesSrc);
            PortableDeviceApiLib.IStream wpdStreamSrc;
            uint            optimalTransferSizeSrc = 0;
            _tagpropertykey propertySrc            = new _tagpropertykey();

            propertySrc.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
            propertySrc.pid   = 0;
            resourcesSrc.GetStream(file.Id, ref propertySrc, 0, ref optimalTransferSizeSrc, out wpdStreamSrc);
            System.Runtime.InteropServices.ComTypes.IStream streamSrc = (System.Runtime.InteropServices.ComTypes.IStream)wpdStreamSrc;

            // WPD destination
            IPortableDeviceContent contentDst;

            this._device.Content(out contentDst);
            IPortableDeviceValues values = this.GetRequiredPropertiesForCopyWPD(file, parentObjectId);

            PortableDeviceApiLib.IStream tempStream;
            uint optimalTransferSizeBytes = 0;

            contentDst.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSizeBytes, null);
            System.Runtime.InteropServices.ComTypes.IStream streamDst = (System.Runtime.InteropServices.ComTypes.IStream)tempStream;

            try {
                unsafe {
                    byte[] buffer = new byte[1024];
                    int    bytesRead;
                    do
                    {
                        // read from source
                        streamSrc.Read(buffer, 1024, new IntPtr(&bytesRead));

                        // write to destination
                        IntPtr pcbWritten = IntPtr.Zero;
                        if (bytesRead > 0)
                        {
                            streamDst.Write(buffer, bytesRead, pcbWritten);
                        }
                    } while (bytesRead > 0);
                }
                success = true;
                streamDst.Commit(0);
            } catch (Exception) {
                success = false;
            } finally {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(tempStream);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(streamSrc);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wpdStreamSrc);
            }

            return(success);
        }
Example #19
0
 public void Add(PropertyKey key, IPortableDevicePropVariantCollection value)
 {
     this.values.SetIPortableDevicePropVariantCollectionValue(key, value);
 }
Example #20
0
        private static void EnumerateFolderContent(ref IPortableDeviceContent content, PortableDeviceFolder parent, string currentFolder, string targetFolder, ref List <wpdFileInfo> retlist, ref string targetFolderID)
        {
            // save ID of the parent when target folder is reached
            if (targetFolder == currentFolder)
            {
                targetFolderID = parent.Id;
            }
            // Get the properties of the object
            IPortableDeviceProperties properties;

            content.Properties(out properties);
            // Enumerate the items contained by the current object
            IEnumPortableDeviceObjectIDs objectIds;

            content.EnumObjects(0, parent.Id, null, out objectIds);
            uint fetched = 0;

            do
            {
                string objectId;
                objectIds.Next(1, out objectId, ref fetched);
                if (fetched > 0)
                {
                    PortableDeviceObject currentObject = WrapObject(properties, objectId);
                    // only collect objects within the targetFolder, ignore all other object
                    if (targetFolder == currentFolder)
                    {
                        // build item
                        string name = currentObject.Name;
                        if (currentObject is PortableDeviceFolder)
                        {
                            // get file modified date
                            IPortableDeviceKeyCollection keys;
                            properties.GetSupportedProperties(currentObject.Id, out keys);
                            IPortableDeviceValues values;
                            properties.GetValues(currentObject.Id, keys, out values);
                            string          objDateStr = "";
                            _tagpropertykey property   = new _tagpropertykey();
                            property.fmtid = new Guid("EF6B490D-5CD8-437A-AFFC-DA8B60EE4A3C");
                            property.pid   = 19;                                 //WPD_OBJECT_DATE_MODIFIED
                            try {
                                values.GetStringValue(property, out objDateStr); // 2016/10/19:18:52:52.000
                            } catch {; }
                            retlist.Add(new wpdFileInfo("p:" + name, "wpd_" + currentObject.Id, 0, objDateStr));
                        }
                        if (currentObject is PortableDeviceFile)
                        {
                            // get file properties
                            IPortableDeviceKeyCollection keys;
                            properties.GetSupportedProperties(currentObject.Id, out keys);
                            IPortableDeviceValues values;
                            properties.GetValues(currentObject.Id, keys, out values);

                            // get file size
                            long            objSize;
                            _tagpropertykey property = new _tagpropertykey();
                            property.fmtid = new Guid("EF6B490D-5CD8-437A-AFFC-DA8B60EE4A3C");
                            property.pid   = 11; //WPD_OBJECT_SIZE;
                            values.GetSignedLargeIntegerValue(property, out objSize);

                            // get file modified date
                            string objDateStr;
                            property.pid = 19;                               //WPD_OBJECT_DATE_MODIFIED
                            values.GetStringValue(property, out objDateStr); // 2016/10/19:18:52:52.000
                            //DateTime objDateUTC = (DateTime.ParseExact(objDateStr, "dd.MM.yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal)).ToUniversalTime();  // 2016/10/19:18:52:52.000

                            // transfer file to return list
                            retlist.Add(new wpdFileInfo("f:" + name, "wpd_" + currentObject.Id, objSize, objDateStr));
                        }
                    }

                    // a path sequence along the targetFolder is needed; as long as currentFolder is the beginning of the targetFolder, we are on the right track
                    string latestFolder = Path.Combine(currentFolder, currentObject.Name);
                    if ((targetFolder.StartsWith(latestFolder)))
                    {
                        // next level starts with a new currentFolder
                        currentFolder = latestFolder;
                        EnumerateFolderContent(ref content, (PortableDeviceFolder)currentObject, currentFolder, targetFolder, ref retlist, ref targetFolderID);
                        // after returning from a deeper level, currentFolder needs to reset to the next higher level
                        currentFolder = Path.GetDirectoryName(currentFolder);
                    }
                }
            } while (fetched > 0);
        }
Example #21
0
        public bool FindSrcDir(string volume, string dirpath, out string retryMsg, MainWindowData mainWnd)
        {
            const string defretryMsg = "機器を接続してOKを選択してください";

            retryMsg = defretryMsg;

            // 接続中のデバイス数を取得
            uint count = 0;

            deviceManager.RefreshDeviceList();
            deviceManager.GetDevices(null, ref count);
            if (count == 0)
            {
                retryMsg = defretryMsg;
                return(false);
            }

            // コピー元デバイス、フォルダの存在チェック
            string[] deviceIds            = new string[count];
            PortableDeviceClass[] devices = new PortableDeviceClass[count];
            deviceManager.GetDevices(deviceIds, ref count);

            var  clientInfo   = (IPortableDeviceValues) new PortableDeviceValuesClass();
            bool existsSrcDir = true;

            foreach (var deviceId in deviceIds)
            {
                PortableDeviceClass device = new PortableDeviceClass();
                try {
                    // デバイス情報の取得
                    IPortableDeviceContent    content;
                    IPortableDeviceProperties properties;
                    device.Open(deviceId, clientInfo);
                    device.Content(out content);
                    content.Properties(out properties);

                    IPortableDeviceValues propertyValues;
                    properties.GetValues("DEVICE", null, out propertyValues);

                    var    property = new _tagpropertykey();
                    string devicename;
                    property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
                    property.pid   = 4;
                    propertyValues.GetStringValue(property, out devicename);
                    //Console.WriteLine(devicename);

                    // 対象デバイスで無ければスキップ
                    if (devicename != volume)
                    {
                        continue;
                    }

                    // コピー元デバイスのアクセス許可状態のチェック
                    var rootObjs = GetObjects("DEVICE", content); // ルートオブジェクトが見えるか
                    if (rootObjs.Count() <= 0)
                    {
                        retryMsg = "対象機器へのアクセスが許可されていません\n" +
                                   "対象機器でMTP転送を有効後にOKを選択してください";
                        return(false);
                    }

                    // コピー元フォルダのデバイスIDを取得
                    existsSrcDir = true;
                    string curDirId = "DEVICE";
                    foreach (string searchdir in dirpath.TrimStart('\\').TrimEnd('\\').Split('\\'))
                    {
                        //Console.WriteLine(searchdir);
                        bool existCurDir = false;
                        var  dirObjs     = GetObjects(curDirId, content, TransFileObject.ObjectKind.DIR);

                        foreach (var dirobj in dirObjs)
                        {
                            if (dirobj.fileName == searchdir)
                            {
                                existCurDir = true;
                                curDirId    = dirobj.objId;
                                break;
                            }
                        }
                        if (!existCurDir)
                        {
                            existsSrcDir = false;
                            break;
                        }
                    }
                    if (existsSrcDir)
                    {
                        srcDirObjId = curDirId;
                        srcDevId    = deviceId;
                        retryMsg    = "";
                        break;
                    }
                }
                catch (Exception e) {
                    mainWnd.DispInfo = string.Format("コピー元の検出失敗\n{0}", e.Message);
                    existsSrcDir     = false;
                }
                finally {
                    device.Close();
                }
            }

            return(existsSrcDir);
        }
Example #22
0
        private TransFileObject WrapObject(IPortableDeviceProperties properties, string objectId)
        {
            IPortableDeviceKeyCollection keys;

            properties.GetSupportedProperties(objectId, out keys);

            IPortableDeviceValues values;

            properties.GetValues(objectId, keys, out values);

            // Get the name of the object
            string name;
            var    property = new _tagpropertykey();

            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid   = 4;

            try {
                values.GetStringValue(property, out name);
            }
            catch (COMException e) {
                name = "(non name)";
            }

            // Get the original name of the object
            string OriginalName;

            property       = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid   = 12;
            try {
                values.GetStringValue(property, out OriginalName);
            }
            catch (COMException e) {
                OriginalName = "";
            }

            // Get last write time
            DateTime updatetime = new DateTime();

            property       = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid   = 19;
            try {
                string value;
                values.GetStringValue(property, out value);
                string format = "yyyy/MM/dd:HH:mm:ss.fff";
                updatetime = DateTime.ParseExact(value, format, null);
            }
            catch (COMException e) {
                //updatetime = DateTime.Now;
            }

            // Get the type of the object
            Guid contentType;

            property       = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid   = 7;
            try {
                values.GetGuidValue(property, out contentType);
            }
            catch (COMException e) {
                return(new TransFileObject(name, null, updatetime, TransFileObject.ObjectKind.DIR));
            }

            Guid folderType     = new Guid(0x27E2E392, 0xA111, 0x48E0, 0xAB, 0x0C, 0xE1, 0x77, 0x05, 0xA0, 0x5F, 0x85);
            Guid functionalType = new Guid(0x99ED0160, 0x17FF, 0x4C44, 0x9D, 0x98, 0x1D, 0x7A, 0x6F, 0x94, 0x19, 0x21);

            if (contentType == folderType || contentType == functionalType)
            {
                return(new TransFileObject(name, objectId, updatetime, TransFileObject.ObjectKind.DIR));
            }

            if (OriginalName.CompareTo("") != 0)
            {
                name = OriginalName;
            }

            return(new TransFileObject(name, objectId, updatetime, TransFileObject.ObjectKind.FILE));
        }
        //delete a file from device
        private static void StringToPropVariant(string value,out PortableDeviceApiLib.tag_inner_PROPVARIANT propvarValue)
        {
            PortableDeviceApiLib.IPortableDeviceValues pValues =
                (PortableDeviceApiLib.IPortableDeviceValues)
                    new PortableDeviceTypesLib.PortableDeviceValuesClass();

            var WPD_OBJECT_ID = new _tagpropertykey();
            WPD_OBJECT_ID.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA,
                         0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_ID.pid = 2;

            pValues.SetStringValue(ref WPD_OBJECT_ID, value);

            pValues.GetValue(ref WPD_OBJECT_ID, out propvarValue);
        }
Example #24
0
        private IPortableDeviceValues GetRequiredPropertiesForContentType(string source,
            string destination, string parentObjectId)
        {
            IPortableDeviceValues values =
                new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

            var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
            WPD_OBJECT_PARENT_ID.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                         0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_PARENT_ID.pid = 3;
            values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

            FileInfo fileInfo = new FileInfo(source);
            var WPD_OBJECT_SIZE = new _tagpropertykey();
            WPD_OBJECT_SIZE.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                         0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_SIZE.pid = 11;
            values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong)fileInfo.Length);

            var WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();
            WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                         0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_ORIGINAL_FILE_NAME.pid = 12;
            values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, Path.GetFileName(destination));

            var WPD_OBJECT_NAME = new _tagpropertykey();
            WPD_OBJECT_NAME.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                         0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_NAME.pid = 4;
            values.SetStringValue(WPD_OBJECT_NAME, Path.GetFileName(destination));

            return values;
        }
        private static PortableDeviceObject WrapObject(IPortableDeviceProperties properties, 
            string objectId)
        {
            IPortableDeviceKeyCollection keys;
            properties.GetSupportedProperties(objectId, out keys);

            IPortableDeviceValues values;
            properties.GetValues(objectId, keys, out values);

            // Get the name of the object
            string name;
            var property = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 4;
            values.GetStringValue(property, out name);

            // Get the type of the object
            Guid contentType;
            property = new _tagpropertykey();
            property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                                      0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            property.pid = 7;
            values.GetGuidValue(property, out contentType);

            var folderType = new Guid(0x27E2E392, 0xA111, 0x48E0, 0xAB, 0x0C,
                                      0xE1, 0x77, 0x05, 0xA0, 0x5F, 0x85);
            var functionalType = new Guid(0x99ED0160, 0x17FF, 0x4C44, 0x9D, 0x98,
                                          0x1D, 0x7A, 0x6F, 0x94, 0x19, 0x21);

            if (contentType == folderType  || contentType == functionalType)
            {
                return new PortableDeviceFolder(objectId, name);
            }

            return new PortableDeviceFile(objectId, name);
        }
 public void SetIntProperty(string objectId, _tagpropertykey propertyKey, int value)
 {
     SetProperty(objectId, propertyKey, values => values.SetSignedIntegerValue(propertyKey, value));
 }
 public void SetBoolProperty(string objectId, _tagpropertykey propertyKey, bool value)
 {
     SetProperty(objectId, propertyKey, values => values.SetBoolValue(propertyKey, value ? 1 : 0));
 }
Example #28
0
        public void DownloadFile(PortableDeviceFile file, string saveToPath)
        {
            IPortableDeviceContent content;
            this._device.Content(out content);

            IPortableDeviceResources resources;
            content.Transfer(out resources);

            PortableDeviceApiLib.IStream wpdStream;
            uint optimalTransferSize = 0;

            var property = new _tagpropertykey();
            property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
            property.pid = 0;

            resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

            System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

            var filename = Path.GetFileName(file.Id);
            FileStream targetStream = new FileStream(Path.Combine(saveToPath, filename), FileMode.Create, FileAccess.Write);

            unsafe
            {
                var buffer = new byte[1024];
                int bytesRead;
                do
                {
                    sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                    targetStream.Write(buffer, 0, 1024);
                } while (bytesRead > 0);
                targetStream.Close();
            }
        }
Example #29
0
 private Command(PropertyKey commandKey)
 {
     this.values = (IPortableDeviceValues) new PortableDeviceValues();
     this.values.SetGuidValue(WPD.PROPERTY_COMMON_COMMAND_CATEGORY, commandKey.fmtid);
     this.values.SetUnsignedIntegerValue(WPD.PROPERTY_COMMON_COMMAND_ID, commandKey.pid);
 }
Example #30
0
        public void DownloadFile(PortableDeviceFile file, out StreamWrapper sourceStream)
        {
            Connect();
            IPortableDeviceContent content = null;
            this._device.Content(out content);

            IPortableDeviceResources resources = null;
            content.Transfer(out resources);

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

            var property = new _tagpropertykey();
            property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
            property.pid = 0;
            resources.Cancel();
            resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

            sourceStream = new StreamWrapper((System.Runtime.InteropServices.ComTypes.IStream)wpdStream);

            //sourceStream = ;
            //unsafe
            //{
            //    var buffer = new byte[1024];
            //    int bytesRead;
            //    do
            //    {
            //        sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
            //        //outputStream.Write(buffer, 0, 1024);
            //    } while (bytesRead > 0);
            //    //targetStream.Close();                
            //}

            //System.Runtime.InteropServices.Marshal.ReleaseComObject(sourceStream);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(wpdStream);

            //sourceStream = null;
            //wpdStream = null;



        }
Example #31
0
 public void Add(PropertyKey key, Guid value)
 {
     this.values.SetGuidValue(key, value);
 }
        //public static PropVariant FromValue(PortableDeviceApiLib.tag_inner_PROPVARIANT value)
        //{
        //    IntPtr ptrValue = Marshal.AllocHGlobal(Marshal.SizeOf(value));
        //    Marshal.StructureToPtr(value, ptrValue, false);

        //    //
        //    // Marshal the pointer into our C# object
        //    //
        //    return (PropVariant)Marshal.PtrToStructure(ptrValue, typeof(PropVariant));
        //}

        private static IPortableDeviceValues GetDeviceValues(IPortableDeviceContent deviceContent, _tagpropertykey key, string objectId)
        {
            IPortableDeviceProperties deviceProperties;

            deviceContent.Properties(out deviceProperties);

            var keyCollection = (IPortableDeviceKeyCollection) new PortableDeviceTypesLib.PortableDeviceKeyCollectionClass();

            keyCollection.Add(key);

            IPortableDeviceValues deviceValues;

            deviceProperties.GetValues(objectId, keyCollection, out deviceValues);
            return(deviceValues);
        }
Example #33
0
 public void Add(PropertyKey key, string value)
 {
     this.values.SetStringValue(key, value);
 }
Example #34
0
        /// <summary>
        ///     Execute the specified command
        /// </summary>
        /// <param name="command"></param>
        public void ExecuteCommand(_tagpropertykey command)
        {
            var commandValues = (IPortableDeviceValues) new PortableDeviceValuesClass();
            IPortableDeviceValues results;

            commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid);
            commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, command.pid);

            // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx)
            PortableDeviceClass.SendCommand(0, commandValues, out results);
        }
Example #35
0
        /**
         * Copy To PC
         */
        public void TransferContentFromDevice(PortableDeviceFile file, string saveToPath, String fileName)
        {
            FileStream targetStream = null;

            try
            {
                // make sure that we are not holding on to a file.
                DisconnectConnect();

                // Make sure that the target dir exists.
                System.IO.Directory.CreateDirectory(saveToPath);

                IPortableDeviceContent content = getContents();

                IPortableDeviceResources resources;
                content.Transfer(out resources);

                PortableDeviceApiLib.IStream wpdStream;
                uint optimalTransferSize = 0;

                var property = new _tagpropertykey();
                property.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42);
                property.pid   = 0;

                resources.GetStream(file.Id, ref property, 0, ref optimalTransferSize, out wpdStream);

                System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;

                // var fileName = Path.GetFileName(file.Id);
                targetStream = new FileStream(Path.Combine(saveToPath, fileName), FileMode.Create, FileAccess.Write);

                // Getthe total size.
                long length  = file.size;
                long written = 0;
                long lPCt    = 0;
                unsafe
                {
                    var buffer = new byte[1024];
                    int bytesRead;
                    do
                    {
                        sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
                        targetStream.Write(buffer, 0, bytesRead);

                        written += 1024;
                        long PCt = length > 0 ? (100 * written) / length : 100;
                        if (PCt != lPCt)
                        {
                            lPCt = PCt;
                            Console.WriteLine("Progress: " + lPCt);
                        }
                    } while (bytesRead > 0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            finally
            {
                if (null != targetStream)
                {
                    targetStream.Close();
                }
                Disconnect();
            }
        }
 public void SetStringProperty(string objectId, _tagpropertykey propertyKey, string value)
 {
     SetProperty(objectId, propertyKey, values => values.SetStringValue(propertyKey, value));
 }