5作为用户窗体应用程序运行进程时无法隐藏CMD窗口

5作为用户窗体应用程序运行进程时无法隐藏CMD窗口

本文介绍了c#.NEt 3.5作为用户窗体应用程序运行进程时无法隐藏CMD窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用支持应用程序中的CMD提示符在后台运行一堆进程.

I am trying to run a bunch of processes in the background using the CMD prompt from my support application.

我已经成功完成了标准命令的操作,但是当尝试以管理员身份运行命令(以检索和结束进程)时,控制台窗口将短暂出现在屏幕上.

I have successfully done this for standard commands, but when trying to run a command as an administrator (to retrieve and end processes), the console window will briefly appear on the screen.

代码:

public static bool checkRunning(string procName)
    {
        var ss = new SecureString();
        ss.AppendChar('T');
        ss.AppendChar('a');
        ss.AppendChar('k');
        ss.AppendChar('e');
        ss.AppendChar('c');
        ss.AppendChar('a');
        ss.AppendChar('r');
        ss.AppendChar('e');
        ss.AppendChar('9');
        ss.AppendChar('9');
        ss.MakeReadOnly();

        //ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/C tasklist /S " + Program.servName + " /FI \"SESSION eq " + Program.sessID + "\" /FO CSV /NH")

        ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C tasklist /S " + Program.servName + " /FI \"SESSION eq " + Program.sessID + "\" /FO CSV /NH";


            startInfo.WorkingDirectory = @"C:\windows\system32";
            startInfo.Verb = "runas";
            startInfo.Domain = "mydomain";
            startInfo.UserName = "ADMINuser";
            startInfo.Password = ss;

            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;


        string strCheck = " ";

        Process proc = Process.Start(startInfo);
        proc.OutputDataReceived += (x, y) => strCheck += y.Data;

        proc.BeginOutputReadLine();
        proc.WaitForExit();

        if (strCheck.Contains(procName))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

据我了解,设置shellExecute = false,createNoWindow = true和ProcessWindowStyle.Hidden应该可以解决此问题,但是我认为由于需要管理员登录,它无法正常工作.

From what I understand, setting shellExecute=false, createNoWindow=true and ProcessWindowStyle.Hidden should resolve the issue, but I believe it is not working due to the required admin log in.

有人知道该问题的解决方案或合适的解决方法吗?任何帮助深表感谢,非常感谢

Does anyone know of a solution or suitable workaround to this problem?Any help is much appreciated,Many thanks

推荐答案

来自 MSDN网站在ProcessStartInfo.CreateNoWindow属性上:

From MSDN site on ProcessStartInfo.CreateNoWindow Property :

没有提及解决方法或解决方案,我一直找不到任何地方.

There is no workaround or resolution mentioned, and I have been unable to find one anywhere.

我不得不依靠我的应用程序在运行某些进程时简要显示CMD窗口(当不使用用户名和密码时,CreateNoWindow属性起作用).

I have had to resort to me application briefly displaying CMD windows when running certain processes (The CreateNoWindow property works when not using UserName and Password).

这篇关于c#.NEt 3.5作为用户窗体应用程序运行进程时无法隐藏CMD窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 23:58