Reputation:
I'm using C# (WPF)
I have full path of process and i start the process with with Process.Start()
command.
Process p = new Process();
p.shartInfo.FileName = fullPathOfFile;
p.Start();
Now, i want to show process that i'm start before (after Start()
command).
i.e if i start the notepad and the user minimize the notepad and click on Start Notepad
in my application i want to check if i running notepad before, if so then show the running notepad to user.
if p is running:
Show p
else
Start p
how can i do it in C#?
Thanks.
Upvotes: 2
Views: 1278
Reputation: 8308
To check if process with following name is already running you can use Process.GetProcessesByName(processName)
method.
In case process is running you can maximize it's window by using Pinvoke
. Call ShowWindow
with SW_SHOWMAXIMIZED
Parameter.
Otherwise you can simply call Process.Start(processName)
to start a process.
Try following
internal class MyClass
{
//use this flag to maximize process window.
const int SW_SHOWMAXIMIZED = 3;
//use this flag to open process window normally.
const int SW_SHOWNORMAL = 1;
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void Main(string[] args)
{
var processName = "notepad";
var process = Process.GetProcessesByName(processName).FirstOrDefault();
if (process != null)
ShowWindow(process.MainWindowHandle, SW_SHOWNORMAL);
else
Process.Start(processName);
}
}
Upvotes: 2
Reputation: 859
After the p.Start() call, p.Id gives you the process id. Later you can use Process.GetProcessById( id ) to find the running process.
To bring its window to front is a bit more complicated task. See http://www.codeproject.com/Articles/2976/Detect-if-another-process-is-running-and-bring-it
Upvotes: 1