15

在你说它是一个重复的问题之前,请让我解释一下(因为我已经阅读了所有类似的主题)。

我的应用程序具有以下两种设置:

  procStartInfo.CreateNoWindow = true;
  procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

并且还有 WindowsApplication 作为输出类型。

当我调用命令行命令时,黑色窗口仍然出现。我还能做些什么来隐藏窗口吗?并非所有命令都发生这种情况,XCOPY 是黑色窗口确实闪烁的情况。这只发生在我 XCOPY 的目的地也已经包含该文件并且它提示我是否要替换它时。即使我传入 /Y 它仍然会短暂闪烁。

如果有帮助,我愿意使用 vbscript,但还有其他想法吗?

客户端将调用我的可执行文件,然后传入命令行命令,即:

C:\MyProgram.exe start XCOPY c:\Test.txt c:\ProgramFiles\

这是应用程序的完整代码:

class Program
{
    static void Main(string[] args)
    {      
            string command = GetCommandLineArugments(args);

            // /c tells cmd that we want it to execute the command that follows and then exit.
            System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + command);

            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;

            // Do not create the black window.
            procStartInfo.CreateNoWindow = true;
            procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = procStartInfo;
            process.Start();

        }

    private static string GetCommandLineArugments(string[] args)
    {
        string retVal = string.Empty;

        foreach (string arg in args)
            retVal += " " + arg;


        return retVal;
    }
}
4

4 回答 4

9

问题是您使用的是 cmd.exe。只有它的控制台窗口将被隐藏,而不是您要求它启动的进程的控制台窗口。使用 cmd.exe 没有什么意义,除非您尝试执行它自己实现的一些命令。像复制一样。

如果您需要 cmd.exe,您仍然可以抑制窗口,您必须使用 /B 选项来启动。输入开始/?在命令提示符下查看选项。并不是说它有帮助,您不能使用 START COPY。

xcopy.exe 中有一个特定的怪癖可能会让你失望。如果您不重定向输入,它不会执行。它只是无法在没有诊断的情况下运行。

于 2010-08-09T14:11:51.533 回答
5

我看到您正在调用cmd然后将命令作为参数传递。而是直接调用命令

例如

    System.Diagnostics.ProcessStartInfo procStartInfo = new System.DiagnosticsProcessStartInfo("xcopy", "<sourcedir> <destdir> <other parameters>");

procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
于 2010-08-09T13:42:01.280 回答
4

您可以尝试添加

process.StartInfo.UseShellExecute = false; 

到你的过程

于 2012-11-14T17:55:55.550 回答
0

我有一个类似的任务 - 可以通过 API 调用在创建后隐藏窗口。(在您的情况下,您可能需要一个辅助线程。)

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

如果您知道新窗口的句柄,您可以调用

ShowWindow(hWnd, 0);

0隐藏窗口,1显示窗口

要获取 Window 的句柄,请查看:

pinvoke.net enumwindows(user32)

于 2015-05-04T15:02:59.713 回答