Exemple #1
0
        public virtual PortableDeviceObject GetContentByPath(string path)
        {
            //path = mtp:\{FriendlyName}\{storage}\...
            //         0       1             2       3
            if (!Helpers.IsMTPPathValid(path))
            {
                throw new ArgumentException();
            }

            var nameArr = path.Split(pathSeparator);

            if (nameArr.Length == 2 && nameArr[1].Equals(FriendlyName))
            {
                return(new PortableDeviceFolder("DEVICE", FriendlyName));
            }

            var storageName = nameArr[2];
            var storage     = GetStorageByName(storageName);

            if (nameArr.Length == 3)
            {
                return(storage);
            }

            PortableDeviceObject parentObject = storage;
            PortableDeviceObject deviceObject = null;

            for (int i = 3; i < nameArr.Length; i++)
            {
                deviceObject = GetContentByNameIn(nameArr[i], parentObject.Id);
                parentObject = deviceObject ?? throw new FileNotFoundException(path + " Can not find the path");
            }

            return(deviceObject);
        }
Exemple #2
0
        public void CreateDirectory(string path)
        {
            if (!IsMTPPath(path))
            {
                throw new MTPException(MTPException.INVALID_PATH);
            }

            var device = GetDeviceFromPath(path);

            if (device == null)
            {
                throw new MTPException(MTPException.NOT_FOUND_DEVICE);
            }

            var pathArr = path.Split(Path.DirectorySeparatorChar);

            if (pathArr.Length == 3) //to storage only
            {
                return;
            }

            bool shouldCreate = false;

            try
            {
                device.Connect();

                PortableDeviceObject parent = device.GetStorageByName(pathArr[2]);
                PortableDeviceObject folder;
                for (int i = 3; i < pathArr.Length; i++)
                {
                    folder = device.GetContentByNameIn(pathArr[i], parent.Id);
                    if (folder == null)
                    {
                        shouldCreate = true;
                    }

                    if (shouldCreate)
                    {
                        folder = device.NewFolder(pathArr[i], parent.Id);
                    }

                    parent = folder;
                }
            }
            catch
            {
                throw new MTPException(MTPException.ERROR_WRITE);
            }
            finally
            {
                device.DisConnect();
            }
        }