Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 749
  • Last Modified:

How to display Select Computer stantard dialog

I'd like to display standard Select Computer dialog, like the one in Computer Management Console -> Right click -> Select "Connect to Another Computer" -> Browse
How can it be done from C#?
Any other standard network browse dialog also will be OK.

Any help will be appreciated,
Jigit
0
Jigit
Asked:
Jigit
1 Solution
 
Fahad MukhtarDistinguished EngineerCommented:
There is no such dialog but you can make your own... eg i am filling all the computers in a listbox:

DirectoryEntry root  = new DirectoryEntry("WinNT:");        
               
foreach (DirectoryEntry dDom in root.Children)                  
      foreach (DirectoryEntry dPC in dDom.Children)                  
            if (dPC.SchemaClassName == "Computer" )
                  listBox1.Items.Add (dDom.Name + "/" + dPC.Name);            

0
 
tomasX2Commented:
     FolderBrowserDialog di = new FolderBrowserDialog();
      di.ShowDialog();
0
 
JigitAuthor Commented:
tomasX2, thank you. Your suggestion allows to browse for folders, while I need only computers.

Desp, your suggestion is closer to what I need. The only problem I encounter with it is performance. The whole enterprise network is being scanned for all the object.

Is there possibility to limit it only to computers?
Or display only domain controllers and expand their networks on mouse click? (Maybe filling them in a tree view)
Does it support WinNT Networks and Active Directory? What has "LDAP" to do with it?

Thanks,
Jigit
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Fahad MukhtarDistinguished EngineerCommented:
you can put a line
Application.Doevents();
to improve the performance (compare the performance by commenting the line and run the code again)

for active directory , LDAP thingy check this:
http://www.c-sharpcorner.com/Code/2002/Aug/ListAllComps.asp
0
 
JigitAuthor Commented:
I'm trying to build my own ComputerBrowserDialog using your suggestions. I'm filling Domain Controllers into TreeView on form load. I'd like to expand a network when user double clicks on it. How can I do it? My code is published below, but it does not react on clicking on a node. Any ideas?

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.DirectoryServices;

namespace MMCTest
{
      /// <summary>
      /// Summary description for BrowseServerForm.
      /// </summary>
      public class BrowseServerForm : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button okButton;
            private System.Windows.Forms.Button cancelButton;
            private System.Windows.Forms.ListBox listBox1;
            public string ServerName = null;
            private System.Windows.Forms.TreeView computersTreeView;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public BrowseServerForm()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.okButton = new System.Windows.Forms.Button();
                  this.cancelButton = new System.Windows.Forms.Button();
                  this.listBox1 = new System.Windows.Forms.ListBox();
                  this.computersTreeView = new System.Windows.Forms.TreeView();
                  this.SuspendLayout();
                  //
                  // okButton
                  //
                  this.okButton.Location = new System.Drawing.Point(392, 352);
                  this.okButton.Name = "okButton";
                  this.okButton.TabIndex = 1;
                  this.okButton.Text = "OK";
                  this.okButton.Click += new System.EventHandler(this.okButton_Click);
                  //
                  // cancelButton
                  //
                  this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                  this.cancelButton.Location = new System.Drawing.Point(160, 360);
                  this.cancelButton.Name = "cancelButton";
                  this.cancelButton.TabIndex = 2;
                  this.cancelButton.Text = "Cancel";
                  this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
                  //
                  // listBox1
                  //
                  this.listBox1.Location = new System.Drawing.Point(32, 40);
                  this.listBox1.Name = "listBox1";
                  this.listBox1.Size = new System.Drawing.Size(344, 225);
                  this.listBox1.TabIndex = 3;
                  //
                  // computersTreeView
                  //
                  this.computersTreeView.ImageIndex = -1;
                  this.computersTreeView.Location = new System.Drawing.Point(392, 48);
                  this.computersTreeView.Name = "computersTreeView";
                  this.computersTreeView.SelectedImageIndex = -1;
                  this.computersTreeView.Size = new System.Drawing.Size(360, 272);
                  this.computersTreeView.TabIndex = 4;
                  //
                  // BrowseServerForm
                  //
                  this.AcceptButton = this.okButton;
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.CancelButton = this.cancelButton;
                  this.ClientSize = new System.Drawing.Size(786, 418);
                  this.Controls.Add(this.computersTreeView);
                  this.Controls.Add(this.listBox1);
                  this.Controls.Add(this.cancelButton);
                  this.Controls.Add(this.okButton);
                  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
                  this.Name = "BrowseServerForm";
                  this.Text = "Browse Server";
                  this.Load += new System.EventHandler(this.BrowseServerForm_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            private void BrowseServerForm_Load(object sender, System.EventArgs e)
            {

                  computersTreeView.BeforeExpand += new TreeViewCancelEventHandler(computersTreeView_BeforeExpand);
                  
                  DirectoryEntry root  = new DirectoryEntry("WinNT:");    
                  foreach (DirectoryEntry dDom in root.Children)
                  {
                        TreeNode node = new TreeNode();
                        node.Text = dDom.Name;
                        computersTreeView.Nodes.Add ( node );    
                  }
            }

            private void okButton_Click(object sender, System.EventArgs e)
            {
                  this.DialogResult = DialogResult.OK;
                  if ( listBox1.SelectedItem != null )
                  {
                        ServerName = listBox1.SelectedItem.ToString();
                  }
            }

            private void cancelButton_Click(object sender, System.EventArgs e)
            {
                  this.DialogResult = DialogResult.Cancel;
            }

            private void computersTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
            {
                  TreeNode node = sender as TreeNode;
                  if ( node == null ) return;

                  DirectoryEntry root  = new DirectoryEntry("WinNT:");        
                  foreach (DirectoryEntry dDom in root.Children)
                  {
                        if ( dDom.Name != node.Text ) continue;

                        foreach (DirectoryEntry dPC in dDom.Children)              
                              if (dPC.SchemaClassName == "Computer" )
                                    node.Nodes.Add ( dPC.Name);      
                  }
            }
      }
}
0
 
eternal_21Commented:
Change:

  computersTreeView.DoubleClick += new TreeViewCancelEventHandler(computersTreeView_BeforeExpand);

to:

  computersTreeView.DoubleClick += new EventHandler(computersTreeView_DoubleClick);


Change:

  private void computersTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)

to:

  private void computersTreeView_DoubleClick(object sender, EventArgs e)


And finally, change:

  TreeNode node = sender as TreeNode;

to:

  TreeNode node = ((TreeView)sender).SelectedNode;


You may also find this of interest: http://www.codeproject.com/csharp/csenumnetworkresources.asp
0
 
JigitAuthor Commented:
eternal_21, thanks for your comment. Let me argue with you: node expansion may be done not only by double-clicking, but also by presssing "+" on the keyboard or maybe some other event. I've already implemented it using BeforeExpand. So all the points go to Desp, who was the first to suggest this solution.

Thanks to all of you , guys!!!
0
 
Fahad MukhtarDistinguished EngineerCommented:
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now