728x90
    
    
  Edge 에서 특정 Tab 을 닫고 싶을때 처리하는 방법입니다.
열려있는 Tab중에 찾고자 하는 Tab 이름이 유일한 경우 처리가 가능합니다.
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Forms;
namespace WpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Process[] procsEdge = Process.GetProcessesByName("msedge");
            foreach (Process Edge in procsEdge)
            {
                if (Edge.MainWindowHandle != IntPtr.Zero)
                {
                    AutomationElement root = AutomationElement.FromHandle(Edge.MainWindowHandle);
                    var tabs = root.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
                    foreach (AutomationElement tabitem in tabs)
                    {
                        if (tabitem.Current.Name.ToUpper().Contains("GOOGLE"))
                        {
                            tabitem.SetFocus();
                            SendKeys.SendWait("^{w}");
                        }
                    }
                }
            }
        }
    }
}
위 내용을 보면 알수 있지만 특정탭을 찾아 Focus 를 가게한후 탭을 닫는 단축키를 실행하여 처리가 됩니다.
WPF .NET6 인 경우 프로젝트 파일에 아래항목이 추가되어야 SendKeys 를 사용할 수 있습니다.
<UseWindowsForms>true</UseWindowsForms>
| <Project Sdk="Microsoft.NET.Sdk">  <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> <Nullable>enable</Nullable> <UseWPF>true</UseWPF> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> </Project>  | 
실행결과

728x90
    
    
  'C#' 카테고리의 다른 글
| [C#] WebView2 설치여부 확인 및 설치되도록 처리하기 (0) | 2022.09.29 | 
|---|---|
| [C#] Chrome 브라우저 Google 사이트 에서 특정 단어 조회페이지 열기 (0) | 2022.09.28 | 
| [C#] Edge Open 하기 (항상 새로운 창으로 띄우기) (0) | 2022.09.23 | 
| [C#] ExpandoObject 사용하기 (0) | 2022.09.15 | 
| [C#] Active Directory Authentication .NET6 (0) | 2022.08.28 |