title: C# .NET 4.0 Drag and drop between two applications
body: <p>So I'd really like to drag and drop data between two instances of an application; however, if there is data present at the target point where I am dropping, I would like to swap that data with what is being dropped.</p>
  
  <p>I'm trying to use a MemoryMappedFIle, and that seems to work most of the time, but it's not perfect. For example, if I do the drag / drop too quickly, the target data is simply overwritten (I assume it's not being written to the MemoryMappedFile quickly enough). Does anyone have any recommendations?﻿</p>
  
  <p>This is what I currently have:</p>
  
  <pre><code>    private void pbSprite_MouseDown(object sender, MouseEventArgs e)
      {
          PictureBox pb = (PictureBox)(sender);
          DataObject data = new DataObject();
          if (pb.Name == pbSprite.Name)
          {
              data = new DataObject(DataFormats.Serializable, frmpkm);
          }
          else
          {
              data = new DataObject(DataFormats.Serializable, frmpkm2);
          }
          pb.DoDragDrop(data, DragDropEffects.Move);
          MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen("name", 1000, MemoryMappedFileAccess.ReadWrite);
          using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
          {
              PKMDS.Pokemon otherpkm = new PKMDS.Pokemon();
              for (int i = 0; i &lt; Marshal.SizeOf(otherpkm); i++)
              {
                  FileMap.Read&lt;byte&gt;(i, out otherpkm.Data[i]);
              }
              if (pb.Name == pbSprite.Name)
              {
                  frmpkm.Data = otherpkm.Data;
              }
              else
              {
                  frmpkm2.Data = otherpkm.Data;
              }
              lblData.Text = frmpkm.SpeciesName;
              lblData2.Text = frmpkm2.SpeciesName;
              pbSprite.Image = frmpkm.Sprite;
              pbSprite2.Image = frmpkm2.Sprite;
          }
      }
      private void pbSprite_DragDrop(object sender, DragEventArgs e)
      {
          if (e.Data != null)
          {
              PictureBox pb = (PictureBox)(sender);
              PKMDS.Pokemon otherpkm = (PKMDS.Pokemon)e.Data.GetData(DataFormats.Serializable);
              MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen("name", 1000, MemoryMappedFileAccess.ReadWrite);
              using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
              {
                  for (int i = 0; i &lt; Marshal.SizeOf(frmpkm); i++)
                  {
                      if (pb.Name == pbSprite.Name)
                      {
                          FileMap.Write&lt;byte&gt;(i, ref frmpkm.Data[i]);
                      }
                      else
                      {
                          FileMap.Write&lt;byte&gt;(i, ref frmpkm2.Data[i]);
                      }
                  }
              }
              if (pb.Name == pbSprite.Name)
              {
                  frmpkm.Data = otherpkm.Data;
              }
              else
              {
                  frmpkm2.Data = otherpkm.Data;
              }
              lblData.Text = frmpkm.SpeciesName;
              lblData2.Text = frmpkm2.SpeciesName;
              pbSprite.Image = frmpkm.Sprite;
              pbSprite2.Image = frmpkm2.Sprite;
          }
      }
  </code></pre>