|
-
October 29th, 2009, 04:20 AM
#1
Close MS Access from C#
Hi Guys,
This is my first post. I'm a somewhat inexperienced C# programmer (I have come from a background of VBA - you have to start somewhere right?) and I'd like to know if its possible to:
i) Check to see if an MS Application is running?
ii) If it is, to close it.
I do have a method of doing this by grabbing a list of running processes, checking if any are the Access application i'm looking for and then killing the Process. However, I was looking for a way to close the Access application use Automation. Any ideas on the best way to do this?
Many thanks,
Ad
-
October 29th, 2009, 08:40 PM
#2
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|