Guidelines

Sample code

Code showing a form that alerts when devices are connected or disconnected.

public partial class Form1 : Form
{
   private RemoteDeviceManager mgr;

   public Form1()
   {
      InitializeComponent();
      mgr = new RemoteDeviceManager();
      mgr.DeviceConnected += mgr_DeviceConnected;
      mgr.DeviceDisconnected += mgr_DeviceDisconnected;
   }

   private void mgr_DeviceConnected(object sender, RemoteDeviceConnectEventArgs e)
   {
      this.OnConnection(e.Device, true);
   }

   private void mgr_DeviceDisconnected(object sender, RemoteDeviceConnectEventArgs e)
   {
      this.OnConnection(e.Device, false);
   }

   private void OnConnection(RemoteDevice dev, bool connected)
   {
      if (connected)
         statusLabel.Text = dev.Name + " is now connected";
      else
         statusLabel.Text = "No devices are connected.";
   }

   private void Form1_Load(object sender, EventArgs e)
   {
      RemoteDevice dev = mgr.Devices.FirstConnectedDevice;
      OnConnection(dev, dev != null);
   }

   private void Form1_FormClosing(object sender, EventArgs e)
   {
      mgr.DeviceConnected -= mgr_DeviceConnected;
      mgr.DeviceDisconnected -= mgr_DeviceDisconnected;
   }
}

Code exercising the library.

using (RemoteDeviceManager r = new RemoteDeviceManager())
{
   using (RemoteDevice dev = r.Devices.FirstConnectedDevice)
   {
      if (dev == null)
         return;

      // Device information
      Console.WriteLine(dev.Name + ":" + dev.Platform);
      Console.WriteLine("Remaining power: {0}%", dev.PowerStatus.BatteryLifePercent);

      // Start and stop a synchronization
      dev.StartSync();
      System.Threading.Thread.Sleep(2000);
      dev.StopSync();

      // Manipulate local and device files & directories
      string myDocs = dev.GetFolderPath(SpecialFolder.MyDocuments);
      string deviceFile = myDocs + @"\Test.txt";
      string localFile = System.IO.Path.GetTempFileName();
      System.IO.File.WriteAllText(localFile, "Testing. 1. 2. 3.");

      RemoteFile.CopyFileToDevice(dev, localFile, deviceFile, true);
      RemoteFile.CopyFileFromDevice(dev, myDocs + @"\Test.txt", localFile, true);
      RemoteDirectory.CreateDirectory(dev, myDocs + @"\TestDir");
      RemoteFile.Copy(dev, myDocs + @"\Test.txt", myDocs + @"\TestDir\test2.txt", true);
      RemoteFile.Move(dev, myDocs + @"\TestDir\test2.txt", myDocs + @"\test2.txt");
      Console.WriteLine(@"{0}: Size={1}, FT={2:u},{3:u},{4:u}, Attr={5}", deviceFile,
         "?",
         RemoteFile.GetCreationTime(dev, deviceFile),
         RemoteFile.GetLastAccessTime(dev, deviceFile),
         RemoteFile.GetLastWriteTime(dev, deviceFile),
         RemoteFile.GetAttribtues(dev, deviceFile));
      RemoteFile.Delete(dev, myDocs + @"\test2.txt");
      RemoteDirectory.Delete(dev, myDocs + @"\TestDir");

      Console.WriteLine("Directory listing:");
      foreach (string fn in RemoteDirectory.GetFiles(dev, myDocs, "*"))
         Console.WriteLine("   " + fn);

      dev.CreateShortcut("Test", @"\Temp\Test.txt");
      Console.WriteLine("Test shortcut = " + dev.GetShortcutTarget("Test"));
      RemoteFile.Delete(dev, "Test");

      // Manipulate the registry
      using (RemoteDevice.DeviceRegistryKey hKey = dev.DeviceRegistryLocalMachine.OpenSubKey("Ident"))
      {
         Console.WriteLine("Device Name: " + hKey.GetValue("Name", string.Empty).ToString());
         using (RemoteDevice.DeviceRegistryKey hKey2 = hKey.CreateSubKey("Test"))
         {
            hKey2.SetValue("StrVal", "test");
            hKey2.SetValue("DWVal", (int)55);
            hKey2.SetValue("ByVal", new byte[] { 0, 1, 2, 3, 4, 5, 6 });
            hKey2.SetValue("MStrVal", new string[] { "str1", "str2" });
            foreach (string item in hKey2.GetValueNames())
            {
             object o = hKey2.GetValue(item, string.Empty);
             if (o is byte[])
               o = string.Join(", ", Array.ConvertAll<byte, string>(o as byte[], delegate(byte b) { return b.ToString(); }));
             if (o is string[])
               o = string.Join(", ", o as string[]);
             Console.WriteLine("Test\\{0} = {1}", item, o);
            }
            hKey2.DeleteValue("StrVal");
         }
         hKey.DeleteSubKey("Test");
      }

      // Start a remote process
      dev.CreateProcess(@"\Windows\calendar.exe", null, ProcessCreationFlags.None);
   }
}