How can i execute msg.exe by C# in windows?

馋奶兔 提交于 2019-12-11 06:27:19

问题


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 "

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();

回答1:


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.




回答2:


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();



回答3:


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

    }


来源:https://stackoverflow.com/questions/42568808/how-can-i-execute-msg-exe-by-c-sharp-in-windows

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