简体   繁体   English

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

[英]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.

相关问题 如何在 C# windows 应用程序的表单加载事件中的 process.start("notepad.exe") 之前播放 windows 媒体播放器 - how to play windows media player before the process.start("notepad.exe") in form load event in C# windows app C# 为什么 Process.Start("notepad.exe" myFile) 正常工作,而 Process.Start("notepad++.exe" myFile) 不工作 - C# Why does Process.Start("notepad.exe" myFile) is working and Process.Start("notepad++.exe" myFile) is not working 用新的替换notepad.exe - Replacing notepad.exe with a new one System.Diagnostics.Process.Start(“ Notepad.exe”); 在实时服务器上不工作 - System.Diagnostics.Process.Start(“Notepad.exe”); is not working on live server Process.Start with UseShellExecute 在前一个关闭窗口后无法将 notepad.exe 窗口带到前台 - Process.Start with UseShellExecute fails to bring notepad.exe window to the foreground after a previous close window 使用notepad.exe在列表视图中打开活动项目 - Opening active item in a listview with notepad.exe 如何获取notepad.exe的确切路径以便关联文件扩展名 - How to get the exact path of notepad.exe in order to associate a file extension 以Notepad.exe的方式打开文件的标志是什么? - Which flags to open a file the way Notepad.exe does? 使用Awesomium从html按钮运行notepad.exe - Run notepad.exe from a html-button using Awesomium 使用 C# 以隐藏模式启动任何文件(非 exe) - Start any file (Not exe) in Hidden Mode using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM