隐藏命令窗口在C#中的应用 [英] Hide Command Window in C# Application

查看:131
本文介绍了隐藏命令窗口在C#中的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你说了一个重复的问题,请让我解释一下(因为我读过所有类似的线程)。

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

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

和是还具有WindowsApplication作为输出类型。

黑色的窗子依旧出现时,我调用命令行命令。还有什么我可以做隐藏窗口?它不会发生的所有命令,XCOPY是这种情况:黑色的窗口不会闪了起来。这只是发生时,虽然目标,我XCOPYing也已经包含了该文件,它提示我是否要替换它。即使我通过/ Y它仍然会短暂地闪烁。

我愿意使用VBScript是否会有所帮助,但任何其他的想法?

客户端会打电话给我的可执行文件,然后通过在命令行命令,即:

C:\ MyProgram.exe开始XCOPY C:\ Test.txt的C:\程序文件\

下面是该应用程序的完整code:

 类节目
{
    静态无效的主要(字串[] args)
    {
            字符串命令= GetCommandLineArugments(参数);

            // / C告诉在cmd,我们希望它来执行下面的命令,然后退出。
            System.Diagnostics.ProcessStartInfo procStartInfo =新System.Diagnostics.ProcessStartInfo(cmd.exe的,/ C+指令);

            procStartInfo.RedirectStandardOutput =真;
            procStartInfo.UseShellExecute = FALSE;

            //不要创建黑色的窗口。
            procStartInfo.CreateNoWindow = TRUE;
            procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            的System.Diagnostics.Process过程=新的System.Diagnostics.Process();
            process.StartInfo = procStartInfo;
            的Process.Start();

        }

    私人静态字符串GetCommandLineArugments(字串[] args)
    {
        字符串retVal的=的String.Empty;

        的foreach(在args串ARG)
            retVal的+ =+ ARG;


        返回retVal的;
    }
}
 

解决方案

现在的问题是,你正在使用的cmd.exe。只有的及其的控制台窗口会被隐藏,而不是为你问它启动过程中的控制台窗口。有一个在使用CMD.EXE,除非你试图执行一些它实现自己的命令小点。比如复制。

您仍然可以燮preSS的窗口,如果你需要的cmd.exe,你将不得不使用启动/ B选项。输入start /?在命令提示符下看选项。不,它可以帮助,您不能使用开始复制。

有一个在XCOPY.EXE一个特定的怪癖,可能会引发你离开为好。它的没有的执行,如果您不同时重定向输入。它只是不带诊断运行。

Before you say its a duplicate question, please let me explain (as I've read all similar threads).

My application has both of these settings:

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

and is also has WindowsApplication as the output type.

The black window STILL comes up when I call a command line command. Is there anything else I can do to hide the window? It doesn't happen for all commands, XCOPY is a situation where it the black window does flash up. This only happens though when the destination I'm XCOPYing too already contains the file and it's prompting me if I want to replace it. Even if I pass in /Y it will still flash briefly.

I'm open to using vbscript if that will help, but any other ideas?

The client will call my executable and then pass in a command line command ie:

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

Here's the full code of the application:

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

解决方案

The problem is that you're using cmd.exe. Only its console window will be hidden, not the console window for the process you ask it to start. There's little point in using cmd.exe, unless you are trying to execute some of the commands it implements itself. Like COPY.

You can still suppress the window if you need cmd.exe, you'll have to use the /B option for Start. Type start /? at the command prompt to see options. Not that it helps, you can't use START COPY.

There's a specific quirk in xcopy.exe that might throw you off as well. It does not execute if you don't also redirect the input. It just fails to run without diagnostic.

这篇关于隐藏命令窗口在C#中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆