HI,
can anyone please let me know how to get the all the available shares on a remote computer using vb.net? I know the ip address of the machine and username and password. I need to get the shares available on that computer. please help me in this regards. If possible please provide some sample code on this.
using System;
using System.Management;
class SharePoint {
public static void Main() {
ListShareProps();
}
static void ListShareProps() {
ManagementPath path = new ManagementPath( );
ManagementClass shares = null;
// apply credentials (only allowed when bindin to remote servers)
ConnectionOptions co = new ConnectionOptions();
co.Username = "administrator";
co.Password = "password";
// co.Authority = "kerberos:celeb"; // use kerberos authentication
// co.Authority = "NTLMDOMAIN:celeb"; // or NTLM
path.Server = "langley"; // use . for local server, else server name
path.NamespacePath = @"root\\CIMV2";
path.RelativePath = @"Win32_Share";
ManagementScope scope = new ManagementScope(path, co); // use (path) for local binds
ObjectGetOptions options = new ObjectGetOptions(null, new TimeSpan(0,0,0,5), true);
try {
shares = new ManagementClass(scope, path, options);
ManagementObjectCollection moc = shares.GetInstances();
foreach(ManagementObject mo in moc)
Console.WriteLine("{0} - {1} ",mo["Name"],
mo["Description"],
mo["Path"]);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally {
if(shares != null)
shares.Dispose();
}
}
}
and here is a quickie port to vb.net as a code behind for ASP.NEt
Imports System
Imports System.Management
Public Class Shares
Sub Page_Load(Sender As System.Object, E As System.EventArgs)
ListShareProps()
End Sub
Public Sub ListShareProps()
Dim Path as ManagementPath = new ManagementPath
Dim Shares as ManagementClass = Nothing
Dim CO as ConnectionOptions = new ConnectionOptions
co.Username = "administrator";
co.Password = "password";
' co.Authority = "kerberos:celeb"; ' use kerberos authentication
' co.Authority = "NTLMDOMAIN:celeb"; ' or NTLM
path.Server = "langley"; ' use . for local server, else server name
path.NamespacePath = @"root\\CIMV2"
path.RelativePath = @"Win32_Share"
Dim Scope as ManagementScope = New ManagementScope(Path, CO)
Dim Options as ObjectGetOptions = new ObjectGetOptions(null, new TimeSpan(0,0,0,5), true)
try
shares = new ManagementClass(Scope, Path, Options)
Dim MOC as ManagementObjectCollection = shares.GetInstances()
Dim MO as ManagementObject
For Each MO in MOC
HTTPContext.Current.Response.Write (MO("Name") & " " & mo("descripotion"))
Next
catch e as Exception
HTTPContext.Current.Response.Write
finally
if Shares is Nothing Shares.dispose()
end try
End Sub
End Class
but in this case only we can see the shares available on windows machines right. but in my network linux machines also are available. then how to see shares on linux machines then? i want solution like net view command does.
net view command is able to show any network machine shares right. but as we mention her win32 shares it only shows windows machine shares as i hope. please tell me how can i see if i have linux machine in my network.
I apologize that I do not know more about network management, shares and Linux so I can’t answer this.
I can tell you how to get a recordset of results for the actual “Net View” Command using SQL Server and ADO.Net. If you create a connection with admin priv to a sql server or MSDE database you can issue the command:
EXEC master…xp_cmdshell ‘net view’
And it will returen a DataReader with the results from the command.
xp_cmdshell is a built in stored procedure in SQL Server for running command line functions. This works for any command, for example you can get a directory listing of the C drive using (not useful, but it illustrates the point :))
Thank you grant,
can you tell me in managementqueryobject we are asking to get select * from win32_shares right. is there any queries avialble like that? can you tell me what are queries available and where can i get that info.
Another solution would be make all of these shares webfolders like you were asking about earlier. just as you can create them using IIS on winblows you can use mod_dav along w/ apache on Linux http://webdav.org/mod_dav/. This way you can access all of your folders using a common protocol (http) and you don’t have to worry about the OS in your code.