Skip to content

Instantly share code, notes, and snippets.

@pedroinfo
Created April 25, 2025 13:59
Show Gist options
  • Select an option

  • Save pedroinfo/437955225b9d75c8fb88b3e7a52d4e5e to your computer and use it in GitHub Desktop.

Select an option

Save pedroinfo/437955225b9d75c8fb88b3e7a52d4e5e to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
public static class WindowHelper
{
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// Tenta abrir uma pasta no Explorer e trazê-la para frente.
/// </summary>
/// <param name="path">Caminho da pasta a ser aberta.</param>
public static void OpenFolderAndBringToFront(string path)
{
try
{
var process = Process.Start("explorer.exe", path);
// Dá um tempo para o Explorer carregar a janela
Thread.Sleep(500);
if (process != null && !process.HasExited)
{
// Tenta colocar a janela na frente
SetForegroundWindow(process.MainWindowHandle);
}
}
catch (Exception ex)
{
Debug.WriteLine("Erro ao abrir pasta: " + ex.Message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment