1

Is there a way to display the windows popup msg by using C#?

I mean by using the windows msg.exe program that can be used in cmd, for example:" msg * Hello "

enter image description here

PD: I know that i can use MessageBox.Show() instead. But i want to know if this is possible :(

I wrote 2 ways to do it but none worked:

Process.Start("cmd.exe","/C msg * Hello");

and...

Process cmd = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = "/C msg * Hello",
        UseShellExecute = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden
    }
};
cmd.Start();
suffuko
  • 143
  • 1
  • 2
  • 12

3 Answers3

3

Did you try adding msg.exe directly?

  Process cmd = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = @"msg.exe",
            Arguments = @"* /v Hello",
            WorkingDirectory = Environment.SystemDirectory;
            WindowStyle = ProcessWindowStyle.Normal
        }
    };
    cmd.Start();
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
2

I encountered the same problem.

This was because the project was configured as "AnyCPU" but the "Prefer 32-bit" option was checked in the "Build" tab of the project configuration. Uncheck that option and the problem will disappear.

Edit: personnaly, I use the following function to locate the binary according to the executable and OS platform (32/64 bits):

public static bool LocateMsgExe(out string returnedMsgPath)
{
    returnedMsgPath = null;
    string[] msgPaths = new string[] { Environment.ExpandEnvironmentVariables(@"%windir%\system32\msg.exe"),
                                     Environment.ExpandEnvironmentVariables(@"%windir%\sysnative\msg.exe") };

    foreach (string msgPath in msgPaths)
    {
        if (File.Exists(msgPath))
        {
            returnedMsgPath = msgPath;
            return true;
        }
    }

    return false;
}

And for invoking it :

if (LocateMsgExe(out string strMsgPath))
{
    Process.Start(strMsgPath, "* \"Hello world!\"");
}

Regards,

damien.

damien
  • 54
  • 4
  • 1
    Cool! That solved the problem. I suppose that msg.exe is 64 bits, so my program can not execute it because it's 32 bits :) – suffuko Jul 01 '19 at 00:24
  • %windir%\system32\Msg.exe is 64 bits version on 64 bits OS but 32 bits on 32 bits OS. On 64 bits OS, there is too a 32 bits version of msg.exe in %windir%\sysnative\msg.exe. – damien Jul 01 '19 at 13:21
0

This is my solution. It consists of a webpage (.aspx) with a listbox (lstComputers), a textbox (txtMessageToSend), a dropdownlist to select the OU that contains the computers that will receive the message and a button (btnSendMessage). This is the code for btnSendMessage on the aspx.cs

protected void btnSendMessage_Click(object sender, EventArgs e)
    {
        string searchbase = ddlZaal.SelectedItem.Text; //This is a dropdownlist to select an OU
        DirectoryEntry entry = new DirectoryEntry("LDAP://OU=" + searchbase + ",OU=YourOU,OU=YourSubOU," + Variables.Domain + ""); //Variables.Domain is specified in the Web.config
        DirectorySearcher mySearcher = new DirectorySearcher(entry);
        mySearcher.Filter = ("(objectClass=computer)");
        foreach (SearchResult result in mySearcher.FindAll())
        {
            DirectoryEntry directoryObject = result.GetDirectoryEntry();
            string computernaam = directoryObject.Properties["Name"].Value.ToString();
            lstComputers.Items.Add(computernaam); //This is a listbox that shows the computernames. To each computer a message is sent.
            string pingnaam = computernaam + "dns.suffix"; //Might be necessary for connecting to the computes in the domain
            string MessageToSend = txtMessageToSend.Text; //The text in this textbox will be the messagetext
            Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo(@"C:\inetpub\wwwroot\PsExec.exe"); //Location of PsExec.exe on the webserver that hosts the web-application.
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.RedirectStandardInput = true;
            psi.WindowStyle = ProcessWindowStyle.Minimized;
            psi.CreateNoWindow = true;
            psi.Arguments = "/accepteula -s -i \\\\" + pingnaam + " cmd /c msg.exe * " + MessageToSend;
            process.StartInfo = psi;
            process.Start();
        }

    }