Re: Close MS Access from C#
Basically the major class to manipulate processes on local system ore remote one is
System.Diagnostics.Process
It has several static methods that allow to enumerate existing processes.
Also any instance of this class has a lot of useful fields for analyzing and manipulating process.
To enumerate all processes and kill MS Access process you can use:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string target_name = "MSACCESS";
Process[] local_procs = Process.GetProcesses();
try
{
Process target_proc = local_procs.First<Process>(p => p.ProcessName == target_name);
}
catch (InvalidOperationException)
{
MessageBox.Show("Process " + target_name + " not found!");
return;
}
target_proc.Kill();
}
}
}
If you confused with lambda syntax:
Process target_proc = local_procs.First<Process>(p => p.ProcessName == target_name);
you can do the same in general way:
Process target_proc = null;
foreach (Process proc in local_procs)
{
if (proc.ProcessName == target_name)
{
target_proc = proc;
break;
}
}
To get process status you can use field proc.HasExited;
Good luck!