【问题标题】:How can i execute msg.exe by C# in windows?如何在 Windows 中通过 C# 执行 msg.exe?
【发布时间】:2017-03-03 01:02:42
【问题描述】:

有没有办法使用 C# 显示窗口弹出消息?

我的意思是使用windows的msg.exe程序可以在cmd中使用,例如:“msg * Hello

PD:我知道我可以使用 MessageBox.Show() 代替。但我想知道这是否可能:(

我写了 2 种方法来做到这一点,但都没有奏效:

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

还有……

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

【问题讨论】:

  • 你为什么不把 msg.exe 作为文件名并把 *Hello 放在你的参数中?将 WindowStyle 更改为不隐藏。
  • 好吧,因为这给了我一个错误:(
  • 你遇到了什么错误?

标签: c# windows cmd process popup


【解决方案1】:

您是否尝试直接添加 msg.exe?

  Process cmd = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = @"msg.exe",
            Arguments = @"* /v Hello",
            WorkingDirectory = Environment.SystemDirectory;
            WindowStyle = ProcessWindowStyle.Normal
        }
    };
    cmd.Start();

【讨论】:

  • 这给了我以下错误:System.ComponentModel.Win32Exception (0x80004005): 系统找不到指定的文件。
  • 你要加@参数吗?
  • 试试 msg.exe 而不是 c:\windows\system32\
  • C:\windows\system32\msg.exe 也无法正常工作:(
  • 或系统32所在的任何驱动器
【解决方案2】:

我遇到了同样的问题。

这是因为项目配置为“AnyCPU”,但在项目配置的“构建”选项卡中选中了“首选 32 位”选项。取消选中该选项,问题就会消失。

编辑:个人而言,我使用以下函数根据可执行文件和操作系统平台(32/64 位)定位二进制文​​件:

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

并调用它:

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

问候,

达米安。

【讨论】:

  • 酷!这解决了问题。我想 msg.exe 是 64 位的,所以我的程序无法执行它,因为它是 32 位 :)
  • %windir%\system32\Msg.exe 在 64 位操作系统上是 64 位版本,但在 32 位操作系统上是 32 位版本。在 64 位操作系统上,%windir%\sysnative\msg.exe 中也有 32 位版本的 msg.exe。
【解决方案3】:

这是我的解决方案。它由一个网页 (.aspx) 和一个列表框 (lstComputers)、一个文本框 (txtMessageToSend)、一个用于选择包含将接收消息的计算机的 OU 的下拉列表和一个按钮 (btnSendMessage) 组成。 这是 aspx.cs 上 btnSendMessage 的代码

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2014-02-20
    • 2016-03-25
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多