[英]How to start notepad.exe in hidden mode in windows application?
I am trying to start notepad.exe in hidden mode like below is the code which i have written:- 我正在尝试以隐藏模式启动notepad.exe,如下所示是我编写的代码:-
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "notepad.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"C:\Users\Sujeet\Documents\test.txt";
}
catch
{
}
but the problem is process(ie notepad.exe) gets successfully started but startInfo.WindowStyle = ProcessWindowStyle.Hidden is not working. 但问题是进程(即notepad.exe)已成功启动,但startInfo.WindowStyle = ProcessWindowStyle.Hidden无法正常工作。 I have surfed net for this problem but was not able to get the proper solution.
我已经为此问题上网了,但是无法获得正确的解决方案。
This version works on my box: 这个版本适用于我的盒子:
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = true;
startInfo.FileName = @"%windir%\system32\notepad.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"C:\Users\Sujeet\Documents\test.txt";
Process.Start(startInfo);
}
catch
{ }
I only get a Win32Exception telling that the file (test.txt) cannot be found, but the process runs, and no window is visible. 我只收到一个Win32Exception,告诉它找不到文件(test.txt),但是该过程正在运行,并且没有可见的窗口。
Take care to exit the process, or the user will end up with invisible processes running. 请小心退出该进程,否则用户最终将运行不可见的进程。
If your application is uncooperative (like calc.exe you mentioned in the comments), you could try the following: 如果您的应用程序不合作(例如您在注释中提到的calc.exe),则可以尝试以下操作:
Define somewhere: 在某处定义:
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
const int SW_SHOW = 5;
const int SW_HIDE = 0;
And then do the following after crating the process: 然后在创建进程之后执行以下操作:
var proc = Process.Start(startInfo);
while (proc.MainWindowHandle == IntPtr.Zero) //note: only works as long as your process actually creates a main window.
System.Threading.Thread.Sleep(10);
ShowWindow(proc.MainWindowHandle, SW_HIDE);
I don't know why the path %windir%\\system32\\calc.exe
does not work, but it works with startInfo.FileName = @"c:\\windows\\system32\\calc.exe";
我不知道为什么路径
%windir%\\system32\\calc.exe
不起作用,但是它与startInfo.FileName = @"c:\\windows\\system32\\calc.exe";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.