c# .NEt 3.5 Unable to Hide CMD Window When Running a Process as User - Form Application

心不动则不痛 提交于 2019-12-11 19:38:53

问题


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.

Code:

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

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


回答1:


You can use following code :

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

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

const int SW_HIDE = 0;
const int SW_SHOW = 5;  

static void Main(string[] args)
        {
            var handle = GetConsoleWindow();
            ShowWindow(handle, SW_HIDE);
        }



回答2:


From MSDN site on ProcessStartInfo.CreateNoWindow Property :

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.

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).



来源:https://stackoverflow.com/questions/33652343/c-sharp-net-3-5-unable-to-hide-cmd-window-when-running-a-process-as-user-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!