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

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

ログ内検索
  • キーワードを複数指定する場合は 半角スペース で区切ってください。
  • 検索条件は、(AND)=[A かつ B] (OR)=[A または B] となっています。
  • [返信]をクリックすると返信ページへ移動します。
キーワード/ 検索条件 /
検索範囲/ 強調表示/ ON (自動リンクOFF)
結果表示件数/ 記事No検索/ ON
大文字と小文字を区別する

No.88492 の関連記事表示

<< 0 >>
■88492  任意のエクスプローラ(フォルダ)を閉じたい
□投稿者/ Ante -(2018/09/03(Mon) 23:29:49)

    分類:[C#] 

    2018/09/03(Mon) 23:30:18 編集(投稿者)

    C#で任意のエクスプローラ(フォルダ)を閉じたいです。

    Windows10環境となります。

    下記ロジックで、開かれているエクスプローラを全て閉じることはできますが、
    タスクバーまで消えて焦りました。

    最悪このロジックで、タスクバー以外の起動しているエクスプローラを消すでも、要件を満たせるので、
    EXPLORERの中身がタスクバーであるという、判断方法を教示頂けると助かります。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;

    namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {

    // DLL インポートの宣言
    [DllImport("kernel32.dll")]
    private extern static int TerminateProcess(IntPtr hProcess, UInt32 uExitCode);

    public Form1() {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
    foreach (Process oProcess in Process.GetProcessesByName("EXPLORER")) {
    TerminateProcess(oProcess.Handle, 1);
    while (!oProcess.HasExited) {
    Application.DoEvents();
    }
    }
    }
    }
    }


    どうぞよろしくお願い致します。
親記事 /過去ログ153より / 関連記事表示
削除チェック/

■88494  Re[1]: 任意のエクスプローラ(フォルダ)を閉じたい
□投稿者/ 魔界の仮面弁士 -(2018/09/04(Tue) 07:14:57)
    No88492 (Ante さん) に返信
    > C#で任意のエクスプローラ(フォルダ)を閉じたいです。
    
    Quit メソッドではどうでしょうか?
    
    
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<dynamic> explorers = new List<dynamic>();
                {
                    dynamic exp = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("{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[0];
                        if (w != null)
                        {
                            if (Path.GetFileName(w.FullName).ToLowerInvariant() == "explorer.exe")
                            {
                                // Windows Explorer だった場合
                                explorers.Add(w);
                            }
                            else
                            {
                                // Internet Explorer だった場合
                                Marshal.ReleaseComObject(w);
                            }
                        }
                    }
                    // COM オブジェクトの処分
                    Marshal.ReleaseComObject(f);
                    Marshal.ReleaseComObject(exp);
                }
    
    
                foreach (var w in explorers)
                {
                    // 閉じるべきウィンドウかどうかの判定材料
                    Console.Write("Handle:");
                    Console.WriteLine(w.HWND);
                    Console.Write("Position:");
                    Console.WriteLine(new Rectangle(w.Left, w.Top, w.Width, w.Height));
                    Console.Write("Location:");
                    Console.WriteLine(w.LocationName);
                    Console.WriteLine(w.LocationURL);
    
                    // 終了させる
                    w.Quit();
                    Console.WriteLine("-------------");
    
                    // Quit の実行有無によらず、COM オブジェクトの処分は必要
                    Marshal.ReleaseComObject(w);
                }
            }
        }
    }
記事No.88492 のレス /過去ログ153より / 関連記事表示
削除チェック/

■88499  Re[2]: 任意のエクスプローラ(フォルダ)を閉じたい
□投稿者/ Ante -(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);
        }
    }
    
    
記事No.88492 のレス /過去ログ153より / 関連記事表示
削除チェック/

■88505  Re[3]: 任意のエクスプローラ(フォルダ)を閉じたい
□投稿者/ 魔界の仮面弁士 -(2018/09/04(Tue) 13:49:33)
    No88499 (Ante さん) に返信
    > 以下の記事を投稿したのも私なのですが、
    > 「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

    過去ログを提示する場合は、こちらの URL の方が良いかも。
    http://bbs.wankuma.com/index.cgi?mode=al2&namber=87984



    > このコードは現在アクティブであるエクスプローラ上のアイコンを中アイコンに設定するかと思いますが、

    いえ、アクティブかどうかは無関係ですよ。

    今回の No88494 のコードで言えば、
      // 終了させる
      w.Quit();
    で終了させる前に、
      dynamic sfv = w.Document;
      sfv.CurrentViewMode = FolderViewMode.Icon;
      sfv.IconSize = 48u; // 16u 〜 256u の範囲で指定
      Marshal.ReleaseComObject(sfv);
    を呼び出せば OK です。


    // CurrentViewMode に渡す列挙値
    enum FolderViewMode : uint
    {
      Icon = 1u,
      SmallIcon = 2u,
      List = 3u,
      Details = 4u,
      Thumbnail = 5u,
      Tile = 6u,
      ThumbStrip = 7u,
      Contents = 8u,
    }
記事No.88492 のレス /過去ログ153より / 関連記事表示
削除チェック/

■88814  Re[4]: 任意のエクスプローラ(フォルダ)を閉じたい
□投稿者/ Ante -(2018/10/01(Mon) 20:54:45)
    魔界の仮面弁士

    遅くなりましたがご回答ありがとうございます。

    なるほど、アクティブか否かは関係ないのですね。
    非表示のプロセスでエクスプローラ起動して、
    sfv.CurrentViewMode = FolderViewMode.Icon;に設定すればいけました。

    勉強になります。
記事No.88492 のレス / END /過去ログ153より / 関連記事表示
削除チェック/



<< 0 >>

パスワード/

- Child Tree -