如何在Windows中通过C#执行msg.exe? [英] How can i execute msg.exe by C# in windows?

查看:200
本文介绍了如何在Windows中通过C#执行msg.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用C#显示Windows弹出消息?

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

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

PD:我知道我可以改用MessageBox.Show().但我想知道这是否可能:(

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

和...

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

推荐答案

我遇到了同样的问题.

这是因为项目被配置为"AnyCPU",但是在项目配置的构建"选项卡中选中了首选32位"选项.取消选中该选项,问题将消失.

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.

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

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

并调用它:

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

此致

达米安.

这篇关于如何在Windows中通过C#执行msg.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆