5

I have three Microsoft Access databases. I want to be able to switch between these programmatically. I have a void method which accepts a string parameter called dbName (my database name).

public void SwitchDatabase(string dbName)
{

}

I know what the MainWindowTitle of my Access Database is and each database has a different MainWindowTitle so I can create an array of the Process class and make this equal so System.Diagnostics.Process.GetProcesses(). I can then loop through my running processes until I find the one where the ProcessName is MSACCESS and the MainWindowTitle is correct like so:

Process[] processList = Process.GetProcesses();

foreach (Process theProcess in processList)
{
    string processName = theProcess.ProcessName;
    string mainWindowTitle = theProcess.MainWindowTitle;
}

Once I find this, I can then grab the Process ID, and now I want to make this process my active window. How do I do this?

JMK
  • 27,273
  • 52
  • 163
  • 280
  • 2
    What if the .laccdb is created because someone opened the database with Access instead of your application? Or if the file exists because the app that opened it crashed before cleaning it? I don't think it is a reliable way of working. – Ignacio Soler Garcia Feb 16 '12 at 11:11
  • Ok, the crux of the problem is switching to a database that I think is open, so if it isn't it will throw an exception that I can handle – JMK Feb 16 '12 at 11:15
  • Combined with the information in Eric's answer below, the following link helped me get this working - http://stackoverflow.com/questions/4566632/maximize-another-process-window-in-net – JMK Feb 16 '12 at 15:07

2 Answers2

14

Eric's answer didn't work for me. I found a better solution here on SO with SetForegroundWindow. First I wondered, why it one time worked, next time it did'n.Then I excluded the current process from the list. So, here's my final version:

static void BringWindowToFront()
{
    var currentProcess = Process.GetCurrentProcess();
    var processes = Process.GetProcessesByName(currentProcess.ProcessName);
    var process = processes.FirstOrDefault(p => p.Id!=currentProcess.Id);
    if (process == null) return;

    SetForegroundWindow(process.MainWindowHandle);
}

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
Community
  • 1
  • 1
Slesa
  • 245
  • 2
  • 11
  • I couldn't figure out why `var process = processes.FirstOrDefault(p => p.Id!=currentProcess.Id);` is checking for inequality. In my understanding, it should be equality check. Please correct. – skjoshi Apr 23 '16 at 15:20
  • @skjoshi if you have run the second instance of your program, you get 2 or more processes with one name but different Id, there is you take non current process in a LINQ. maybe i am late a bit, i just sayin' – Vadym Buhaiov Jun 13 '18 at 08:53
3

Try this:

[DllImport("user32.dll", CharSet=CharSet.Auto,ExactSpelling=true)]
public static extern IntPtr SetFocus(HandleRef hWnd);


[TestMethod]
public void PlayAround()
{
    Process[] processList = Process.GetProcesses();

    foreach (Process theProcess in processList)
    {
        string processName = theProcess.ProcessName;
        string mainWindowTitle = theProcess.MainWindowTitle;
        SetFocus(new HandleRef(null, theProcess.MainWindowHandle));
    }

}
Eric
  • 391
  • 1
  • 8