Exemplo n.º 1
0
        /// <summary>
        /// デバイス製造元を取得する
        /// </summary>
        /// <param name="deviceId">デバイスID</param>
        public string GetDeviceManufacturer(string deviceId)
        {
            PortableDeviceManager manager = new PortableDeviceManager();
            uint   length       = 0;
            string manufacturer = String.Empty;

            ushort[] usManufacturerer = null;
            try
            {
                manager.GetDeviceManufacturer(deviceId, null, ref length);
            }
            catch (Exception)
            {
                return(manufacturer);
            }

            if (length > 0)
            {
                usManufacturerer = new ushort[length];
                manager.GetDeviceManufacturer(deviceId, usManufacturerer, ref length);
                manufacturer = ushortArrayToString(usManufacturerer);
            }
            Marshal.ReleaseComObject(manager);

            return(manufacturer);
        }
Exemplo n.º 2
0
        /// <summary>
        /// デバイス説明を取得する
        /// </summary>
        /// <param name="deviceId">デバイスID</param>
        public virtual string GetDeviceDescription(string deviceId)
        {
            PortableDeviceManager manager = new PortableDeviceManager();
            uint   length      = 0;
            string description = String.Empty;

            ushort[] usDescription = null;
            try
            {
                manager.GetDeviceDescription(deviceId, null, ref length);
            }
            catch (Exception)
            {
                return(description);
            }

            if (length > 0)
            {
                usDescription = new ushort[length];
                manager.GetDeviceDescription(deviceId, usDescription, ref length);
                description = ushortArrayToString(usDescription);
            }
            Marshal.ReleaseComObject(manager);

            return(description);
        }
Exemplo n.º 3
0
        public static PortableDevice[] GetDevices()
        {
            uint numberOfDevices = 1;

            PortableDeviceManager.GetDevices(null, ref numberOfDevices);
            string[] deviceIDs = new string[numberOfDevices];

            PortableDeviceManager.GetDevices(deviceIDs, ref numberOfDevices);

            return(deviceIDs.Select(id => new PortableDevice(id)).ToArray());
        }
Exemplo n.º 4
0
        /// <summary>
        /// 枚举所有便携式设备(MTP模式)
        /// </summary>
        /// <returns>返回设备id数组</returns>
        public string[] EnumerateDeviceIds()
        {
            string[] deviceIds   = null;
            uint     deviceCount = 1; //设备数目初始值必须大于0

            deviceManager = new PortableDeviceManager();
            deviceManager.GetDevices(null, ref deviceCount);    //获取设备数目必须置第一个参数为null
            if (deviceCount > 0)
            {
                deviceIds = new string[deviceCount];
                deviceManager.GetDevices(ref deviceIds[0], ref deviceCount);
            }

            return(deviceIds);
        }
Exemplo n.º 5
0
        private bool CheckForMTP()
        {
            PortableDeviceManager deviceManager = new PortableDeviceManager();

            deviceManager.RefreshDeviceList();
            uint numberOfDevices = 1;

            deviceManager.GetDevices(null, ref numberOfDevices);
            string temp1 = "";

            deviceManager.GetDevices(ref temp1, ref numberOfDevices);
            if (temp1 != "")
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 6
0
        public Device(PortableDeviceManager devices, string id)
        {
            this.id = id;

            uint length = 0;

            devices.GetDeviceDescription(id, null, ref length);

            char[] bytes = new char[length];
            devices.GetDeviceDescription(id, bytes, ref length);
            description = new string(bytes, 0, (int)length - 1);

            foobalator.Log.WriteLine(string.Format("Discovered {0}", description));

            PortableDeviceValues values = new PortableDeviceValues();

            values.SetUnsignedIntegerValue(Constants.WPD_CLIENT_DESIRED_ACCESS, Constants.GENERIC_READ | Constants.GENERIC_WRITE);

            PortableDevice device = new PortableDevice();

            device.Open(id, values);
            device.Content(out content);
            content.Properties(out properties);

            DeviceObject root = new DeviceObject(this, "DEVICE");

            DeviceObject phoneFolder = root.GetChildByName("Phone");

            if (phoneFolder == null)
            {
                return;
            }

            musicFolder = phoneFolder.GetChildByName("Music");
            if (musicFolder == null)
            {
                return;
            }

            playlistFolder = musicFolder.GetChildByName("Playlist");
            if (playlistFolder == null)
            {
                return;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// デバイスプロトコルを取得する
        /// </summary>
        /// <param name="deviceId">デバイスID</param>
        public virtual string GetDeviceProtocol(string deviceId)
        {
            PortableDeviceManager manager = new PortableDeviceManager();
            string deviceProtocol         = string.Empty;

            try
            {
                var isOpened = IsOpened();
                if (!isOpened)
                {
                    Open(deviceId);
                }
                IPortableDeviceContent    content;
                IPortableDeviceProperties properties;
                device.Content(out content);
                content.Properties(out properties);
                PortableDeviceApiLib.IPortableDeviceValues propertyValues;
                properties.GetValues("DEVICE", null, out propertyValues);
                propertyValues.GetStringValue(WpdProperty.WPD_DEVICE_PROTOCOL, out deviceProtocol);
                if (Marshal.IsComObject(propertyValues))
                {
                    Marshal.ReleaseComObject(propertyValues);
                }
                if (Marshal.IsComObject(properties))
                {
                    Marshal.ReleaseComObject(properties);
                }
                if (Marshal.IsComObject(content))
                {
                    Marshal.ReleaseComObject(content);
                }
                if (!isOpened)
                {
                    Close();
                }
            }
            catch (Exception)
            {
                deviceProtocol = string.Empty;
            }
            Marshal.ReleaseComObject(manager);

            return(deviceProtocol);
        }
Exemplo n.º 8
0
        /// <summary>
        /// デバイス種別を取得する
        /// </summary>
        /// <param name="deviceId">デバイスID</param>
        public virtual DeviceType GetDeviceType(string deviceId)
        {
            PortableDeviceManager manager = new PortableDeviceManager();
            uint deviceType = (uint)DeviceType.Unknown;

            try {
                var isOpened = IsOpened();
                if (!isOpened)
                {
                    Open(deviceId);
                }
                IPortableDeviceContent    content;
                IPortableDeviceProperties properties;
                device.Content(out content);
                content.Properties(out properties);
                PortableDeviceApiLib.IPortableDeviceValues propertyValues;
                properties.GetValues("DEVICE", null, out propertyValues);
                propertyValues.GetUnsignedIntegerValue(WpdProperty.WPD_DEVICE_TYPE, out deviceType);
                if (Marshal.IsComObject(propertyValues))
                {
                    Marshal.ReleaseComObject(propertyValues);
                }
                if (Marshal.IsComObject(properties))
                {
                    Marshal.ReleaseComObject(properties);
                }
                if (Marshal.IsComObject(content))
                {
                    Marshal.ReleaseComObject(content);
                }
                if (!isOpened)
                {
                    Close();
                }
            }
            catch (Exception)
            {
                deviceType = (uint)DeviceType.Unknown;
            }
            Marshal.ReleaseComObject(manager);

            return((DeviceType)deviceType);
        }
Exemplo n.º 9
0
        private void LoadFiles()
        {
            PortableDeviceManager deviceManager = new PortableDeviceManager();

            deviceManager.RefreshDeviceList();
            uint numberOfDevices = 1;

            deviceManager.GetDevices(null, ref numberOfDevices);
            string[] deviceIds;
            string   temp1 = "";

            deviceIds = new string[numberOfDevices];
            deviceManager.GetDevices(ref temp1, ref numberOfDevices);
            PortableDevices.PortableDevice Drive = null;
            try
            {
                if (temp1 != "")
                {
                    PortableDevices.PortableDevice Device = new PortableDevices.PortableDevice(temp1);
                    try
                    {
                        Device.Connect();
                        string temp = Device.FriendlyName;
                        PortableDeviceFolder root   = Device.GetContents();
                        PortableDeviceFolder folder = root.Files.First() as PortableDeviceFolder;
                        foreach (string file in Directory.EnumerateFiles("Files"))
                        {
                            Device.TransferContentToDeviceFromStream(Path.GetFileName(file), new MemoryStream(File.ReadAllBytes(file)), folder.Id);
                        }
                    }
                    catch (Exception ex)
                    {
                        Device.Disconnect();
                        MessageBox.Show("Unable to load files Has this one already been loaded?");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hit Exception:" + ex.ToString());
            }
            MessageBox.Show("Offload Complete! please unplug drive");
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            string libraryFile = args.Length > 0 ? args[0] : iTunesLibrary.DefaultLibrary;

            iTunesLibrary library = new iTunesLibrary();
            bool          exists  = library.Parse(libraryFile);

            if (!exists)
            {
                return;
            }

            if (args.Length > 1)
            {
                string destDir = args[1];

                foreach (var playlist in library.playlists)
                {
                    if (playlist.Tracks.Count > 200)
                    {
                        continue;
                    }

                    CopyTracks(playlist.Tracks, destDir);
                }
            }
            else
            {
                string[] deviceIDs = new string[1];
                uint     count     = (uint)deviceIDs.Length;

                PortableDeviceManager devices = new PortableDeviceManager();
                devices.GetDevices(deviceIDs, ref count);
                if (count == 0)
                {
                    foobalator.Log.WriteLine("No device connected");
                    return;
                }

                Device device = new Device(devices, deviceIDs[0]);
                device.Sync(library);
            }
        }
Exemplo n.º 11
0
        public List<Device> Get()
        {
            List<Device> connectedPortableDevices = new List<Device>();
            PortableDeviceManager manager = new PortableDeviceManager();

            manager.RefreshDeviceList();
            uint count = 1;
            manager.GetDevices(null, ref count);

            if (count == 0) return connectedPortableDevices;

            // Call the above again because we now know how many devices there are.

            string[] deviceIds = new string[count];
            manager.GetDevices(ref deviceIds[0], ref count);

            ExtractDeviceInformation(deviceIds, connectedPortableDevices);
            return connectedPortableDevices;
        }
Exemplo n.º 12
0
        public string GetFriendlyName(string deviceID)
        {
            string name       = string.Empty;
            uint   nameLength = 20;

            ushort[] nameBuffer = new ushort[nameLength];

            deviceManager = new PortableDeviceManager();
            deviceManager.GetDeviceFriendlyName(deviceID, ref nameBuffer[0], ref nameLength);

            for (int i = 0; i < nameLength; i++)
            {
                if (nameBuffer[i] != 0)
                {
                    name += (char)nameBuffer[i];
                }
            }

            return(name);
        }
Exemplo n.º 13
0
        /// <summary>
        /// 接続されているデバイスのID配列を取得する
        /// </summary>
        /// <returns></returns>
        public virtual string[] GetDeviceIds()
        {
            // デバイスマネージャ
            PortableDeviceManager manager = new PortableDeviceManager();

            manager.RefreshDeviceList();
            // 接続されているデバイス数を取得する
            uint deviceNum = 1;

            manager.GetDevices(null, ref deviceNum);
            // デバイスIDを取得する
            string[] deviceIds = new string[deviceNum];
            if (deviceNum > 0)
            {
                manager.GetDevices(deviceIds, ref deviceNum);
            }
            // 解放
            Marshal.ReleaseComObject(manager);

            return(deviceIds);
        }
Exemplo n.º 14
0
        public static System.Collections.Generic.IEnumerable <ShellObjectInfoItemProvider> From(ShellObjectInfo shellObjectInfo, Predicate <ShellObjectInfoEnumeratorStruct> func)
        {
            ThrowIfNull(shellObjectInfo, nameof(shellObjectInfo));

            System.Collections.Generic.IEnumerable <ShellObject>     shellObjects;
            System.Collections.Generic.IEnumerable <IPortableDevice> portableDevices;

            shellObjects = (System.Collections.Generic.IEnumerable <ShellObject>)shellObjectInfo.InnerObjectGeneric;

            System.Collections.Generic.IEnumerable <ShellObjectInfoItemProvider> getShellObjects(in System.Collections.Generic.IEnumerable <IPortableDevice> _portableDevices) => GetShellObjects(shellObjects, _portableDevices, shellObjectInfo.ClientVersion, func);

            if (shellObjectInfo.InnerObjectGeneric.ParsingName == Computer.ParsingName)
            {
                var portableDeviceManager = new PortableDeviceManager();

                portableDeviceManager.GetDevices();

                portableDevices = portableDeviceManager.PortableDevices;

                //System.Collections.Generic.IEnumerable<ShellObjectInfoItemProvider> getShellItems() =>
                // if (shellObjects == null) return GetPortableDevices(portableDevices, shellObjects, clientVersion, func);

                /*else*/
                //;

                ShellObjectInfoItemProvider getNewShellObjectInfoItemProvider(in NonShellObjectRootItemType nonShellObjectRootItemType) => new ShellObjectInfoItemProvider(nonShellObjectRootItemType, shellObjectInfo.ClientVersion);

                return((portableDevices == null
                        ? getShellObjects(null)
                        : getShellObjects(portableDevices).AppendValues(GetPortableDevices(portableDevices, shellObjects, shellObjectInfo.ClientVersion, func)))
                       .AppendValues(new ShellObjectInfoItemProvider(ShellObjectFactory.Create(RecycleBin.ParsingName), shellObjectInfo.ClientVersion),
                                     getNewShellObjectInfoItemProvider(NonShellObjectRootItemType.Registry),
                                     getNewShellObjectInfoItemProvider(NonShellObjectRootItemType.WMI)));
            }

            else
            {
                return(getShellObjects(null));
            }
        }
Exemplo n.º 15
0
 void updateMTPDrives()
 {
     textBox1.AppendText("MTP devices:\n");
     bool anyFound = false;
     try
     {
         var deviceManager = new PortableDeviceManager();
         deviceManager.RefreshDeviceList();
         var deviceIds = new string[1];
         uint count = 1;
         deviceManager.GetDevices(ref deviceIds[0], ref count);
         deviceIds = new string[count];
         deviceManager.GetDevices(ref deviceIds[0], ref count);
         foreach (var deviceId in deviceIds)
         {
             var str = new MTPDevice(deviceId).FriendlyName;
             bool found = false;
             foreach (string name in usbNames)
             {
                 if (name.CompareTo(str) == 0)
                 {
                     found = true;
                     usbNames.Remove(name);
                     break;
                 }
             }
             if (!found)
             {
                 textBox1.AppendText(str + "\n");
                 anyFound = true;
             }
         }
     }
     catch (Exception e)
     {
         Thread.Sleep(100);
     }
     if (!anyFound)
         textBox1.AppendText("No devices connected");
 }
Exemplo n.º 16
0
        public static WpdDeviceCollection Create()
        {
            var deviceManager = new PortableDeviceManager();

            var deviceIdCount = 0U;

            deviceManager.GetDevices(null, ref deviceIdCount);

            var collection = new WpdDeviceCollection();

            if (deviceIdCount > 0)
            {
                var deviceIds = new string[deviceIdCount];
                deviceManager.GetDevices(deviceIds, ref deviceIdCount);

                foreach (var deviceId in deviceIds)
                {
                    collection.Add(new WpdDevice(deviceId));
                }
            }

            return(collection);
        }
Exemplo n.º 17
0
        public List <Device> Get()
        {
            List <Device>         connectedPortableDevices = new List <Device>();
            PortableDeviceManager manager = new PortableDeviceManager();

            manager.RefreshDeviceList();
            uint count = 1;

            manager.GetDevices(null, ref count);

            if (count == 0)
            {
                return(connectedPortableDevices);
            }

            // Call the above again because we now know how many devices there are.

            string[] deviceIds = new string[count];
            manager.GetDevices(ref deviceIds[0], ref count);

            ExtractDeviceInformation(deviceIds, connectedPortableDevices);
            return(connectedPortableDevices);
        }
Exemplo n.º 18
0
        /// <summary>
        /// デバイス名を取得する
        /// </summary>
        /// <param name="deviceId">デバイスID</param>
        public virtual string GetDeviceFriendlyName(string deviceId)
        {
            PortableDeviceManager manager = new PortableDeviceManager();
            uint   length       = 0;
            string friendlyName = String.Empty;

            ushort[] usFriendlyName = null;
            try {
                manager.GetDeviceFriendlyName(deviceId, null, ref length);
            }
            catch (Exception) {
                return(friendlyName);
            }

            if (length > 0)
            {
                usFriendlyName = new ushort[length];
                manager.GetDeviceFriendlyName(deviceId, usFriendlyName, ref length);
                friendlyName = ushortArrayToString(usFriendlyName);
            }
            Marshal.ReleaseComObject(manager);

            return(friendlyName);
        }
Exemplo n.º 19
0
 public PortableDeviceCollection()
 {
     _deviceManager = new PortableDeviceManager();
 }
Exemplo n.º 20
0
 public TransFromMTPDevice()
 {
     deviceManager = new PortableDeviceManager();
 }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            using var portableDeviceManager = new PortableDeviceManager();

            portableDeviceManager.GetDevices();

            int i;

            for (i = 0; i < portableDeviceManager.PortableDevices.Count; i++)
            {
                Console.WriteLine($"{i}: {portableDeviceManager.PortableDevices[i].DeviceFriendlyName}");
            }

            Console.WriteLine("Please choose a device.");

            if (uint.TryParse(Console.ReadLine(), out uint uintResult) && uintResult < i)
            {
                IPortableDevice portableDevice = portableDeviceManager.PortableDevices[(int)uintResult];

                portableDevice.Open(new ClientVersion("PortableDeviceTests", 1, 0, 0), new PortableDeviceOpeningOptions(GenericRights.Read | GenericRights.Write, FileShareOptions.Read | FileShareOptions.Write, false));

                i = 0;

                var b = new ArrayBuilder <IPortableDeviceObject>();

                foreach (IPortableDeviceObject portableDeviceObject in portableDevice)
                {
                    if (portableDeviceObject.Type == new Guid(Microsoft.WindowsAPICodePack.PortableDevices.Guids.PropertySystem.ContentType.FunctionalObject))
                    {
                        _ = b.AddLast(portableDeviceObject);

                        Console.WriteLine($"{i++}: {portableDeviceObject.Name}");
                    }
                }

                Console.WriteLine("Please enter a memory id.");

                if (uint.TryParse(Console.ReadLine(), out uintResult) && uintResult < i)
                {
                    i = 0;

                    var enumerable = (IEnumerablePortableDeviceObject)b.ToArray()[(int)uintResult];

                    b.Clear();

                    foreach (IPortableDeviceObject portableDeviceObject in enumerable)
                    {
                        if (portableDeviceObject is IPortableDeviceFolder folder)
                        {
                            _ = b.AddLast(folder);

                            Console.WriteLine($"{i++}: {folder.Name}");
                        }
                    }

                    Console.WriteLine("Enter the id of the action to perform on the device.");
                    Console.WriteLine("0: Transfer content.");
                    Console.WriteLine("1: Delete content.");

                    string _menuId = Console.ReadLine();

                    if (int.TryParse(_menuId, out int menuId))
                    {
                        if (menuId == 0)
                        {
                            Console.WriteLine("Please enter the id of the folder to copy the file to.");

                            if (uint.TryParse(Console.ReadLine(), out uintResult) && uintResult < i)
                            {
                                var _folder = (IPortableDeviceFolder)b.ToArray()[(int)uintResult];

                                b.Clear();

                                Console.WriteLine("Please enter a file to copy to the portable device.");

                                string path = Console.ReadLine();

                                Console.WriteLine("Enter the file content type GUID.");

                                string contentType = Console.ReadLine();

                                if (Guid.TryParse(contentType, out Guid guidContentType))
                                {
                                    Console.WriteLine("Enter the file format GUID.");

                                    string format = Console.ReadLine();

                                    if (Guid.TryParse(format, out Guid guidFormat))
                                    {
                                        var stream = new FileStream(path, FileMode.Open);

                                        uint totalWritten = 0;

                                        _folder.PortableDeviceObjectAdded += PortableDevice_PortableDeviceObjectAdded;

                                        _folder.TransferTo(stream, 4000, false, guidContentType, guidFormat, written =>
                                        {
                                            Console.WriteLine($"{written} bytes written during the last write operation; {(totalWritten += written)} total. Continue (Y/y: yes; other input: no)?");

                                            return(true); // Console.ReadLine().ToUpper() == "Y"
                                        });
                                    }
                                }

                                i = 0;

                                foreach (IPortableDeviceObject file in _folder)
                                {
                                    if (file is IPortableDeviceFile _file)
                                    {
                                        _ = b.AddLast(_file);

                                        Console.WriteLine($"{i++}: {_file.Name}");
                                    }
                                }

                                if (uint.TryParse(Console.ReadLine(), out uintResult) && uintResult < i)
                                {
                                    var portableDeviceFile = (IPortableDeviceFile)b.ToArray()[(int)uintResult];

                                    b.Clear();

                                    Console.WriteLine("Please enter a destination file.");

                                    path = Console.ReadLine();

                                    path = $"{System.IO.Path.GetDirectoryName(path)}\\{Path.GetFileNameWithoutExtension(path)} - Copy{Path.GetExtension(path)}";

                                    uint totalWritten = 0;

                                    var stream = new FileStream(path, FileMode.CreateNew);

                                    portableDeviceFile.TransferFrom(stream, 4000, false, written =>
                                    {
                                        Console.WriteLine($"Written: {written}; total: {(totalWritten += written)}.");

                                        return(true);
                                    });

                                    stream.Flush();

                                    stream.Dispose();
                                }
                            }
                        }

                        else if (menuId == 1)
                        {
                            Console.WriteLine("Please enter the id of the folder of the file to delete.");

                            if (uint.TryParse(Console.ReadLine(), out uintResult) && uintResult < i)
                            {
                                var _folder = (IPortableDeviceFolder)b.ToArray()[(int)uintResult];

                                b.Clear();

                                if (_folder.Count == 1)
                                {
                                    if (_folder[0] is IPortableDeviceFile item)
                                    {
                                        _folder.PortableDeviceObjectRemoved += PortableDevice_PortableDeviceObjectRemoved;

                                        item.Delete();
                                    }

                                    else
                                    {
                                        Console.WriteLine("The folder of the given id does not contain a file.");
                                    }
                                }

                                else
                                {
                                    Console.WriteLine("The folder of the given id is empty or contains more than one file.");
                                }
                            }
                        }

                        else
                        {
                            Console.WriteLine("Incorrect menu id.");
                        }
                    }

                    else
                    {
                        Console.WriteLine("Incorrect menu id.");
                    }
                }
            }

            else
            {
                Console.WriteLine("Incorrect device id.");
            }
        }
Exemplo n.º 22
0
 public MobileDeviceManager()
 {
     this._deviceManager = new PortableDeviceManager();
     this.recentFiles    = new Dictionary <string, PortableDeviceObject>();
 }
Exemplo n.º 23
0
 private MtpDeviceManager()
 {
     _deviceManager = new PortableDeviceManager();
 }
 public WindowsPortableDevicesService()
 {
     deviceManager = new PortableDeviceManagerClass();
 }
Exemplo n.º 25
0
 public DeviceManager()
 {
     _hPortableDeviceManager = new PortableDeviceManager();
     _hClientDeviceValues    = ClientValues;
 }
Exemplo n.º 26
0
 /// <summary>
 /// Creates a new instance of media manager.
 /// <para>Remember that this instance is tidly connected with <see cref="PortableDeviceManager"/>
 /// instance and its parent STA thread. Do not try to call this object outside it's apartment,
 /// it will not work. You will get <see cref="COMException"/>.</para>
 /// <para><see cref="MediaManager"/> creates its own instance of COM portable device manager
 /// so that you are able to fork MTP connection from any STA thread.</para>
 /// <para>To use it, create a new instance of this manager and start fetching devices.</para>
 /// </summary>
 public MediaManager()
 {
     this.portableDeviceManager = new PortableDeviceManager();
 }
Exemplo n.º 27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PortableDeviceCollection"/> class.
 /// </summary>
 public PortableDeviceCollection(Logger logger)
 {
     _logger             = logger;
     this._deviceManager = new PortableDeviceManager();
 }