public void Update(ref IPortableDeviceContent pContent, PortableDeviceContainerObject parent)
 {
     lock (dispatcher)
     {
         Enumerate(ref pContent, parent.ID, parent, new AllObjectEnumerateHelper(), detectNewObjects: true);
     }
 }
        /// <summary>
        ///     Inspired by http://cgeers.com/2012/04/17/wpd-transfer-content-to-a-device/
        /// </summary>
        /// <param name="parentObject"></param>
        /// <param name="sourceStream"></param>
        /// <param name="name"></param>
        /// <param name="originalFileName"></param>
        /// <param name="size"></param>
        public PortableDeviceObject Push(PortableDeviceContainerObject parentObject, Stream sourceStream, string name, string originalFileName, ulong size, PushProgressReport progressReport)
        {
            IPortableDeviceContent content;

            portableDeviceClass.Content(out content);
            IPortableDeviceValues values = GetRequiredPropertiesForPush(parentObject, name, originalFileName, size);

            IStream tempStream;
            uint    optimalTransferSizeBytes = 0;

            content.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSizeBytes, null);

            var targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream;

            try
            {
                int totalBytesRead = 0;
                if (progressReport != null)
                {
                    progressReport(totalBytesRead);
                }

                var buffer = new byte[optimalTransferSizeBytes];

                int bytesRead;
                do
                {
                    bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes);
                    IntPtr pcbWritten = IntPtr.Zero;

                    if (bytesRead < (int)optimalTransferSizeBytes)
                    {
                        targetStream.Write(buffer, bytesRead, pcbWritten);
                    }
                    else
                    {
                        targetStream.Write(buffer, (int)optimalTransferSizeBytes, pcbWritten);
                    }

                    totalBytesRead += bytesRead;
                    if (progressReport != null)
                    {
                        progressReport(totalBytesRead);
                    }
                } while (bytesRead > 0);

                targetStream.Commit(0);

                device.Update(ref content, parentObject);

                // TODO There is no IPortableDeviceDataStream in C# port to get ID, so we will make a bicycle
                return(Find("^" + originalFileName + "$", parentObject).First());
            }
            finally
            {
                Marshal.ReleaseComObject(tempStream);
            }
        }
        private static bool ParentContainsChildsId(PortableDeviceContainerObject node, string objectID)
        {
            string copyId = objectID; // to remove different behavior in compiled code of nested foreach
            var    q      = from child in node.Childs
                            where child.ID == copyId
                            select child;

            return(q.Any());
        }
예제 #4
0
        private void ExploreDeviceDirectory(PortableDeviceContainerObject container, SynchronizedDirectory parentDirectory, int remaingDepthToIgnore)
        {
            if (remaingDepthToIgnore == 0)
            {
                parentDirectory.ObjectOnDevice = container;
            }

            foreach (var child in container.Childs)
            {
                var fileObject = child as PortableDeviceFileObject;
                if (fileObject != null)
                {
                    if (remaingDepthToIgnore > 0)
                    {
                        continue;
                    }

                    var file = new SynchronizedFile();
                    file.Parent = parentDirectory;
                    file.Name   = fileObject.FileName;
                    file.ComputePath();
                    file.SizeOnDevice   = fileObject.Size;
                    file.IsOnDevice     = true;
                    file.ObjectOnDevice = fileObject;

                    parentDirectory.Files.Add(file);
                }
                else
                {
                    var containerObject = child as PortableDeviceContainerObject;
                    if (containerObject != null)
                    {
                        if (remaingDepthToIgnore > 0)
                        {
                            ExploreDeviceDirectory(containerObject, parentDirectory, remaingDepthToIgnore - 1);
                        }
                        else
                        {
                            var directory = new SynchronizedDirectory();
                            directory.Parent = parentDirectory;
                            directory.Name   = containerObject.Name;
                            directory.ComputePath();
                            directory.IsOnDevice     = true;
                            directory.ObjectOnDevice = containerObject;

                            ExploreDeviceDirectory(containerObject, directory, remaingDepthToIgnore - 1);

                            parentDirectory.Directories.Add(directory);
                        }
                    }
                }
            }
        }
        public PortableDeviceObject Mkdir(PortableDeviceContainerObject parentObject, string name)
        {
            IPortableDeviceContent content;

            portableDeviceClass.Content(out content);

            var values = (IPortableDeviceValues) new PortableDeviceValues();

            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_PARENT_ID, parentObject.ID);
            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, name);
            values.SetGuidValue(PortableDevicePKeys.WPD_OBJECT_CONTENT_TYPE, PortableDeviceGuids.WPD_CONTENT_TYPE_FOLDER);
            string objId = String.Empty;

            content.CreateObjectWithPropertiesOnly(values, ref objId);

            device.Update(ref content, parentObject);

            var q = from obj in parentObject.Childs
                    where obj.ID == objId
                    select obj;

            return(q.First());
        }
        private void Enumerate(ref PortableDeviceApiLib.IPortableDeviceContent pContent, string parentID, PortableDeviceContainerObject node)
        {
            PortableDeviceApiLib.IPortableDeviceProperties properties;
            pContent.Properties(out properties);

            PortableDeviceApiLib.IEnumPortableDeviceObjectIDs pEnum;
            pContent.EnumObjects(0, parentID, null, out pEnum);

            uint cFetched = 0;
            PortableDeviceObject current;

            do
            {
                string objectID;
                pEnum.Next(1, out objectID, ref cFetched);

                if (cFetched > 0)
                {
                    current = this.ExtractInformation(properties, objectID);
                    node.AddChild(current);
                    if (current is PortableDeviceContainerObject)
                    {
                        Enumerate(ref pContent, objectID, (PortableDeviceContainerObject)current);
                    }
                }
            } while (cFetched > 0);
        }
예제 #7
0
        ///循环监测是否有Pad插入
        private void LoopDetection()
        {
            //LogHelper.LogWrite("MTP服务开启!");
            //创建同步历史记录XML


            while (true)
            {
                ///获取接入的Pad信息
                portableDevices = new ObservableCollection <PortableDevice>();

                if (PortableDeviceCollection.Instance == null)
                {
                    try
                    {
                        PortableDeviceCollection.CreateInstance(AppName, AppMajorVersionNumber, AppMinorVersionNumber);
                        PortableDeviceCollection.Instance.AutoConnectToPortableDevice = false;
                    }
                    catch (Exception ex)
                    {
                        LogHelper.LogWrite(ex.Message);
                    }
                }
                ///添加Pad设备,一次只能接入一个Pad设备
                foreach (var device in PortableDeviceCollection.Instance.Devices)
                {
                    if (device.DeviceId == padOne || device.DeviceId == padTwo)
                    {
                        portableDevices.Add(device);
                    }
                }
                if (portableDevices.Count >= 1)//需增加判断,是否远程WCF服务已开启??
                {
                    if ((portableDevices[0].DeviceId == padOne || portableDevices[0].DeviceId == padTwo) && isUpdateComplete == false)
                    {
                        foreach (var portableDevice in portableDevices)
                        {
                            string[] paths = padSourcePath.Split('\\');
                            try
                            {
                                portableDevice.ConnectToDevice(AppName, AppMajorVersionNumber, AppMinorVersionNumber);
                            }
                            catch//(Exception ex)
                            {
                                //LogHelper.LogWrite(ex.Message);
                                continue;
                            }
                            portableDevice.ScanContent(paths);


                            PortableDeviceContainerObject lastObj = portableDevice.GetLast(portableDevice.Content);

                            if (lastObj.Name == "uploadRefresh")
                            {
                                portableDevice.ScanFolderEnumerate(lastObj.ID, lastObj);

                                //从Pad向PC复制文件
                                try
                                {
                                    portableDevice.CopyFolderToPC(lastObj, pcTargetFolder);
                                }
                                catch (Exception ex)
                                {
                                    LogHelper.LogWrite(ex);
                                }


                                ///查找Pad端指定位置
                                string[] paths1 = padTargetFolder.Split('\\');

                                portableDevice.ScanContent(paths1);

                                lastObjDown = portableDevice.GetLast(portableDevice.Content);

                                //find the all files in the last folder
                                portableDevice.ScanFolderEnumerate(lastObjDown.ID, lastObjDown);

                                //copy folder from pc to pad
                                //portableDevice.CopyFolderToPad(lastObjDown, pcSourceFolder);
                                if (Directory.Exists(pcTargetFolder + "\\uploadRefresh"))
                                {
                                    ///执行数据更新工作
                                    StartUpdateDate();
                                }


                                bool complete = true;
                                ///判断是否更新成功
                                do
                                {
                                    if (n == 6)
                                    {
                                        ///上传同步记录
                                        if (portableDevices[0].DeviceId == padOne)
                                        {
                                            portableDevices[0].CopyFolderToPad(lastObjDown, historyRecordXmlOne);
                                        }
                                        if (portableDevices[0].DeviceId == padTwo)
                                        {
                                            portableDevices[0].CopyFolderToPad(lastObjDown, historyRecordXmlTwo);
                                        }

                                        ///上传结束标志文件
                                        using (FileStream wcfFile = new FileStream(wcfdownloadpath + "time.xml", FileMode.Create))
                                        {
                                            byte[] bytes = new byte[0];
                                            wcfFile.Write(bytes, 0, bytes.Length);
                                            wcfFile.Flush();
                                        }
                                        try
                                        {
                                            portableDevices[0].CopyFolderToPad(lastObjDown, wcfdownloadpath + "time.xml");
                                        }
                                        catch (Exception ex)
                                        {
                                            LogHelper.LogWrite(ex.Message);
                                        }
                                        complete = false;
                                        n        = 0;
                                        if (wcfclient != null)
                                        {
                                            try
                                            {
                                                wcfclient.Close();
                                            }
                                            catch (Exception ex)
                                            {
                                                wcfclient.Abort();
                                                LogHelper.LogWrite(ex);
                                            }
                                        }
                                    }
                                    Thread.Sleep(100);
                                }while (complete);

                                isUpdateComplete = true;
                            }
                        }
                    }
                }
                if (portableDevices.Count == 0)
                {
                    isUpdateComplete = false;
                }
                if (portableDevices != null)
                {
                    portableDevices = null;
                }

                GC.Collect();
                Thread.Sleep(100);
            }
        }
        internal void Enumerate(ref IPortableDeviceContent pContent, string parentID, PortableDeviceContainerObject node, IObjectEnumerateHelper helper, bool detectNewObjects = false)
        {
            IPortableDeviceProperties properties;

            pContent.Properties(out properties);

            foreach (var objectID in ExtractObjectIds(pContent, parentID))
            {
                if (detectNewObjects && ParentContainsChildsId(node, objectID))
                {
                    continue;
                }

                PortableDeviceObject current = ExtractInformation(properties, objectID);
                if (!helper.IsObjectMatching(current))
                {
                    continue;
                }

                node.AddChild(current);

                if (!helper.IsLastNode && current is PortableDeviceContainerObject)
                {
                    Enumerate(ref pContent, objectID, (PortableDeviceContainerObject)current, helper.Next(), detectNewObjects);
                }
            }
        }
 /// <summary>
 ///     Finds collection of specified objects in specific storage service of WPD device
 ///     /android/music/mp3
 ///     /.*/.*/mp3
 ///     /android/sic
 /// </summary>
 /// <param name="path">Represents a '/' delimeted path; each node can be RegEx pattern, not wildcard pattern; </param>
 /// <param name="storage"></param>
 /// <returns></returns>
 public IEnumerable <PortableDeviceObject> Find(string path, PortableDeviceContainerObject parent)
 {
     return(FindInternal(parent.Childs, new PathEnumerateHelper(path)));
 }
 public PortableDeviceObject Push(PortableDeviceContainerObject parentObject, Stream sourceStream, string originalFileName, ulong size, PushProgressReport progressReport)
 {
     return(Push(parentObject, sourceStream, originalFileName, originalFileName, size, progressReport));
 }