C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 153 を表示中)
■88499 / )  Re[2]: 任意のエクスプローラ(フォルダ)を閉じたい
□投稿者/ Ante (13回)-(2018/09/04(Tue) 11:51:51)
No88494 (魔界の仮面弁士 さん) に返信
ありがとうございます。これで要件を満たせます。

よろしければ、もう一つだけ可能でしたらお知らせいただけますか、

以下の記事を投稿したのも私なのですが、
「Windows10 C# エクスプローラー フォルダのレイアウト中アイコン設定」
http://bbs.wankuma.com/search.cgi?no=0&word=%92%86%83A%83C%83R%83%93&andor=and&logs=.%2Fpost.dat&PAGE=20

教示頂いた以下のコードを参考にさせていただきました。

このコードは現在アクティブであるエクスプローラ上のアイコンを中アイコンに設定するかと思いますが、
これをアクティブでない任意のフォルダを中アイコンに設定することは可能なのでしょうか。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var sizes = new Dictionary<uint, string>
        {
            {256u, "特大"},
            {96u, "大"},
            {48u, "中"},
            {16u, "小"},
        };

        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox1.DataSource = sizes.ToArray();
        comboBox1.ValueMember = "Key";
        comboBox1.DisplayMember = "Value";
        comboBox1.SelectedIndex = 2;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (var w in GetExplorer())
        {
            if(Confirm(w.Path, w.LocationName))
            {
                var sfv = w.Document;
                sfv.CurrentViewMode = 1u;
                sfv.IconSize = comboBox1.SelectedValue;
                Marshal.ReleaseComObject(sfv);
            }
        }
    }

    private bool Confirm(string path, string location)
    {
        return DialogResult.Yes == MessageBox.Show(
            this,
            "このフォルダのアイコンサイズを変更しますか?\r\n" + location + "\r\n" + path,
            "確認",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Question,
            MessageBoxDefaultButton.Button2);
    }

    private IEnumerable<dynamic> GetExplorer()
    {
        dynamic exp = Activator.CreateInstance(Type.GetTypeFromCLSID(Guid.Parse("{9BA05972-F6A8-11CF-A442-00A0C90A8F39}")));
        System.Runtime.InteropServices.ComTypes.IEnumVARIANT f = exp._NewEnum;
        object[] varRes = new object[1];
        while (f.Next(1, varRes, IntPtr.Zero) == 0)
        {
            dynamic w = varRes.FirstOrDefault();
            if (w != null)
            {
                if (Path.GetFileName(w.FullName).ToLowerInvariant() == "explorer.exe")
                {
                    yield return w;
                }
                if (Marshal.IsComObject(w))
                {
                    Marshal.ReleaseComObject(w);
                }
            }
        }
        if(Marshal.IsComObject(f))
        {
            Marshal.ReleaseComObject(f);
        }
        Marshal.ReleaseComObject(exp);
    }
}


返信 編集キー/


管理者用

- Child Tree -