2026-06-05 13:44:39 +09:00

60 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace AuroraUIComponent.Util.WindowAPI
{
internal class WindowUtil
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_RESTORE = 9;
public static void OpenOrActivateFolder(string folderPath)
{
Process[] processes = Process.GetProcessesByName("explorer");
bool folderOpened = false;
foreach (var process in processes) {
if (process.MainWindowTitle.Contains(folderPath)) {
folderOpened = true;
} else {
var dirName = folderPath.Split('\\').LastOrDefault();
if (!string.IsNullOrEmpty(dirName) && process.MainWindowTitle.Contains(dirName)) {
folderOpened = true;
}
}
if (folderOpened) {
// 이미 열려 있는 경우
if (IsIconic(process.MainWindowHandle)) {
ShowWindow(process.MainWindowHandle, SW_RESTORE); // 창 복원
}
SetForegroundWindow(process.MainWindowHandle); // 창을 맨 위로
folderOpened = true;
break;
}
}
if (!folderOpened) {
// 폴더가 열려 있지 않은 경우 새로 엽니다.
Process.Start("explorer.exe", folderPath);
}
}
}
}