我正在为我们公司创建一个应用程序启动器,并且我想使用 TreeNode 控件(我们有 100 个需要结构的网络应用程序),当用户单击一个节点(示例:应用程序 1)时,我想运行它自己的程序,即应用程序启动器不等待它关闭等。
我该怎么做?我目前拥有的只是 AD 中的 TreeNode 结构,除了以下代码之外没有任何代码:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
非常感谢
可以使用静态Process方法Start ()
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
// Starts Internet Explorer
Process.Start("iexplore.exe");
// Starts the application with the same name as the TreeNode clicked
Process.Start(e.Node.Text);
}
如果您也希望传递参数,请查看使用ProcessStartInfo类。
您将得到的唯一延迟是等待流程开始。程序运行时,您的代码不会阻塞。
我建议至少需要双击或Enter
按键来启动应用程序,而不仅仅是选择。否则,当用户只是单击以获得焦点或使用箭头键导航树时会发生什么?混乱。
在 TreeViewEventArgs 中,您可以找到受影响的节点:e.Node
Ian 已经指出了如何启动流程。
使用 ProcessStartInfo 让您可以更好地控制应用程序
创建 TreeView 节点时,将应用程序的完整路径放在每个 TreeNode.Tag 属性中并检索它以运行您的进程
using System.Diagnostics;
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//Retrieving the node data
TreeNode myClickedNode = (TreeNode)sender;
//The pointer to your new app
ProcessStartInfo myAppProcessInfo = new ProcessStartInfo(myClickedNode.Tag);
//You can set how the window of the new app will start
myAppProcessInfo.WindowStyle = ProcessWindowStyle.Maximized;
//Start your new app
Process myAppProcess = Process.Start(myAppProcessInfo);
//Using this will put your TreeNode app to sleep, something like System.Threading.Thread.Sleep(int miliseconds) but without the need of telling the app how much it will wait.
myAppProcess.WaitForExit();
}
对于所有属性,请查看MSDN ProcessStartInfo Class和 MSDN Process Class