有没有办法使用C#显示Windows弹出消息?
我的意思是使用可以在cmd中使用的windows msg.exe 程序,例如:" msg *您好"
PD:我知道我可以使用MessageBox.Show()代替。但我想知道这是否可行:(
我写了两种方法来做但没有一种方法:
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();
答案 0 :(得分:2)
您是否尝试直接添加msg.exe?
Process cmd = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = @"msg.exe",
Arguments = @"* /v Hello",
WorkingDirectory = Environment.SystemDirectory;
WindowStyle = ProcessWindowStyle.Normal
}
};
cmd.Start();
答案 1 :(得分:1)
我遇到了同样的问题。
这是因为项目被配置为“ AnyCPU”,但是在项目配置的“构建”选项卡中选中了“首选32位”选项。取消选中该选项,问题将消失。
编辑:个人而言,我使用以下函数根据可执行文件和OS平台(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!\"");
}
此致
达米安。
答案 2 :(得分:0)
这是我的解决方案。它由一个带有列表框(lstComputers)的网页(.aspx),一个文本框(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();
}
}