C# - Clipboardのformatを調べるツールつくった

ExcelとかWordの形式を調べて結果をまとめようかと思ったが、面倒すぎたので、調べるために作ったツールだけ置いてみる。

# 画面キャプチャ

Excel2016で、
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/438183/2a2980e4-210d-c3f4-beb7-9779d9763a3f.png)
を選択してコピーした場合の結果が下図。
`Csv`はなぜか`String`ではなく`MemoryStream`らしい。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/438183/ac868384-5bad-d07d-f2b0-810a77cff037.png)

# ソースコード

```csharp

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

class ClipboardTest : Form
{
    ListView lsv;
    TextBox txtDestFilepath;
    Button btnGetList;
    Button btnSaveFile;

    ClipboardTest()
    {        
        txtDestFilepath = new TextBox();
        txtDestFilepath.Text = (new FileInfo("out_dat")).FullName;
        txtDestFilepath.ReadOnly = true;
        txtDestFilepath.Width = 400;
        Controls.Add(txtDestFilepath);

        btnGetList = new Button();
        btnGetList.Text = "Get Format List";
        btnGetList.Size = new Size(150, 30);
        btnGetList.Location = new Point(0,40);
        btnGetList.Click += (sender,e)=>{ListupClipboardFormat();};
        Controls.Add(btnGetList);

        btnSaveFile = new Button();
        btnSaveFile.Text = "Save as selected format";
        btnSaveFile.Size = new Size(150, 30);
        btnSaveFile.Location = new Point(0,80);
        btnSaveFile.Click += (sender,e)=>{SaveClipboardData();};
        btnSaveFile.Enabled = false;
        Controls.Add(btnSaveFile);

        lsv = new ListView();
        lsv.Location = new Point(0, 120);
        lsv.Size = new Size(400,400);
        lsv.View = View.Details;
        lsv.FullRowSelect = true;
        lsv.GridLines = true;
        lsv.HideSelection = false;
        lsv.Columns.Add("Format", 200);
        lsv.Columns.Add("Class",  180);
        Controls.Add(lsv);

        ClientSize = new Size(400,560);
    }
    
    
    void SaveClipboardData()
    {
        var items = lsv.SelectedItems;
        if (items.Count != 1) {
            return;
        }

        try {
            btnSaveFile.Enabled = false;
            string fmt = items[0].SubItems[0].Text;
            //Console.WriteLine(fmt);
            SaveClipboardDataAs(txtDestFilepath.Text, fmt);
        }
        finally {
            btnSaveFile.Enabled = true;
        }
    }

    static void SaveClipboardDataAs(string destPath, string fmt)
    {
        IDataObject data = Clipboard.GetDataObject();

        object t = data.GetData(fmt);
        if (t is string) {
            File.WriteAllText(destPath+".txt", t as string);
            //Console.WriteLine(t);
        }
        else if (t is MemoryStream) {
            var ms = t as MemoryStream;
            using ( var fs = new FileStream(destPath+".dat", FileMode.Create) ) {
                ms.WriteTo(fs);
            }
        }
        else if (t is Bitmap) {
            Bitmap bmp = t as Bitmap;
            bmp.Save(destPath+".png", ImageFormat.Png);
        }
        else {
            MessageBox.Show("Not supported.");
        }
    }
    
    void ListupClipboardFormat()
    {
        lsv.Items.Clear();
        lsv.BeginUpdate();
        try {
            IDataObject data = Clipboard.GetDataObject();
            if (data != null) {
                foreach (string fmt in data.GetFormats()) {
                    object tmp = data.GetData(fmt);
                    string typeName = "<null>";
                    if ( tmp != null ) {
                        typeName = tmp.GetType().ToString();
                    }
                    lsv.Items.Add(new ListViewItem(new string[]{fmt,typeName}));
                    Console.WriteLine(fmt+"\t"+typeName);
                }
            }
        }
        finally {
            lsv.EndUpdate();
        }
        if ( lsv.Items.Count >= 1 ) {
            btnSaveFile.Enabled = true;
        }
    }

    [STAThread]
    static void Main(string[] args)
    {
        Application.Run(new ClipboardTest());
    }
}

```

# 追記 - HTML形式において文字化けするケースがある

⇒[こちらの記事](https://qiita.com/kob58im/items/7570e94ec399e4ae0874)に記載しています。