繁体   English   中英

如何在Windows应用程序中以隐藏模式启动notepad.exe?

[英]How to start notepad.exe in hidden mode in windows application?

我正在尝试以隐藏模式启动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
{ 

}

但问题是进程(即notepad.exe)已成功启动,但startInfo.WindowStyle = ProcessWindowStyle.Hidden无法正常工作。 我已经为此问题上网了,但是无法获得正确的解决方案。

这个版本适用于我的盒子:

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
{ }

我只收到一个Win32Exception,告诉它找不到文件(test.txt),但是该过程正在运行,并且没有可见的窗口。

请小心退出该进程,否则用户最终将运行不可见的进程。

如果您的应用程序不合作(例如您在注释中提到的calc.exe),则可以尝试以下操作:

在某处定义:

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
    const int SW_SHOW = 5;
    const int SW_HIDE = 0;

然后在创建进程之后执行以下操作:

    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);

我不知道为什么路径%windir%\\system32\\calc.exe不起作用,但是它与startInfo.FileName = @"c:\\windows\\system32\\calc.exe";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM