コード例 #1
0
        public void DeleteFile(PortableDevice device, String name)
        {
            PortableDeviceFile fileToDelete = null;

            try
            {
                foreach (var file in Files)
                {
                    if (file is PortableDeviceFile && name.Equals(file.Name))
                    {
                        fileToDelete = (PortableDeviceFile)file;
                        break;
                    }
                }

                // Got file?
                if (null != fileToDelete)
                {
                    device.DisconnectConnect();
                    device.DeleteFile(fileToDelete);
                    Files.Remove(fileToDelete);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                device.Disconnect();
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: user20112/USBLoader
        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");
        }