Reputation: 545
I have some fairly straightforward code to open up files using a Process object:
var proc = new Process();
proc.StartInfo.FileName = attachmentPath;
proc.StartInfo.UseShellExecute = true;
proc.Start();
Every once in a while, the window that opens from this process starting open behind all my currently open windows. I don't see any pattern or consistency to why this happens. Does anybody have an idea why this happens, or how I can work around it? Thanks!
Upvotes: 2
Views: 541
Reputation: 22571
Windows has discouraged applications from stealing focus for a while; the rules around it aren't really documented, presumably to prevent applications from working around them.
However, you could give this a try:
var proc = new Process();
proc.StartInfo.FileName = attachmentPath;
proc.StartInfo.UseShellExecute = true;
proc.Start();
//Wait for window to spin up
proc.WaitForInputIdle();
BringWindowToTop(proc.MainWindowHandle);
Define BringWindowToTop with p/invoke.
Upvotes: 1