Monday, November 29, 2010

Enumerate Shares in C# ASP.NET using WMI

This does as described. It'll enumerate the shares of any remote machine, and list them. This invokes WMI, and I've incorporated the functionality into my MobileExec 2011 program.



 if (words[0].ToString().ToLower() == "shares")
                {


                    this.Label1.Style["text-align"] = "left";
                    Label1.ForeColor = Color.Black;
                    Label1.ForeColor = Color.Black;
                    Label1.Text = "Sorry! Shares could not be found!";
                    ConnectionOptions co = new ConnectionOptions();
                    co.Username = txtUsername.Text;
                    co.Password = txtPassword.Text;
                    co.Impersonation = ImpersonationLevel.Impersonate;


                    co.EnablePrivileges = true;
                    co.Authentication = AuthenticationLevel.PacketPrivacy;
                    //"Select * from Win32_Directory where Name = 'c:\\Scripts'"
                     string queryStr = "Select * From Win32_Share";


                    System.Net.IPHostEntry h =
                System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
                    string IPAddress = h.AddressList.GetValue(0).ToString();
                    string lm = System.Net.Dns.GetHostName().ToString();




                    if (txtUsername.Text == "" || txtPassword.Text == ""
                 || txtServer.Text == "127.0.0.1"
                 || txtServer.Text.ToLower() == "localhost"
                 || txtServer.Text.ToLower() == lm.ToLower()
                 || txtServer.Text.ToLower() == IPAddress.ToLower())
                    {
                        co.Username = null;
                        co.Password = null;
                    }


                    string temp1 = txtServer.Text;
                    System.Management.ManagementScope ms =
                        new System.Management.ManagementScope
                            ("\\\\" + temp1 + "\\root\\cimv2", co);


                    ObjectQuery oQuery = new ObjectQuery(queryStr);
                   //Execute the query  
                    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(ms, oQuery);
                 
                    //Get the results
                    ManagementObjectCollection oReturnCollection = oSearcher.Get();
                  
                    Label1.Text = "Shares for <b>" +
                       temp1 + "</b><br />------------------------------<br />";


                 
                    foreach (ManagementObject oReturn in oReturnCollection)
                    {
                        Label1.Text += oReturn["Name"] + "<br />";
                        // oReturn.Dispose();
                    }
                }
                //end of shares

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...