c#.NEt 3.5作为用户窗体应用程序运行进程时无法隐藏CMD窗口 [英] c# .NEt 3.5 Unable to Hide CMD Window When Running a Process as User - Form Application

查看:149
本文介绍了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 :

备注

如果UseShellExecute属性为true或用户名和密码 属性不为null,则忽略CreateNoWindow属性值 并创建一个新窗口.

Remarks

If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.

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

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窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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