|
It's possible to get the ip from all the machines that ServerComboBox obtain? How can I do it?
|
|
|
|
|
It is good stuff.
...although I see not all servers of mine network...
|
|
|
|
|
I'm running the demo, and am getting 0 items. There are many SQL Servers over the network, but they don't appear on the list.
Thought it was a firewall issue, but I disabled the firewall locally, and my local instance of SQL Server is not appearing either.
Any ideas?
|
|
|
|
|
Hie,
i need to know how to enumerate all pcs.. connected locally to the users computer.. also i need to display this information in a treeview.. .. i need to show the workgroups and the pcs in them....and also list all the mapped drives .... i have C# 2005 .. .NET 2.0 so i'll need to know how to do it with System.Net(i think.. i jst started C#)
would someone give me a WELL COMMNTED example code.. or perhaps a link to an article or tutorial??
Tried lots of other forums so PLEASE HELP
Gideon
|
|
|
|
|
Dear Phil.
Thanks for yuor posting. It is exactly what I was looking for.
I would like to use your server type combo box in my thesis, as I need to enumerate a drop down for the available MS SQL SERVERS within the network.
However, I need a written permission from you allowing me to do so.
Would you kindly send me an email on stefsam@keyworld.net?
Thanks.
Kind regards.
Stefan Sammut
Malta.
|
|
|
|
|
This code is great, for me just has a slight problem:
For example on list SQL servers, if the server is on the same machine that this code is running and there is no network connectivity, the server is not listed...
Is this a problem of the code or the SQL server should have some option selected for this to work?
Thanks and keep on the good work...
João(Portugal)
|
|
|
|
|
I had the same problem and traced into the code:
int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH,
out entriesRead,
out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out
resHandle);
//if the returned with a NERR_Success (C++ term), =0 for C#
if (ret == 0)
{
//loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
for (int i = 0; i < totalEntries; i++)
{
totalEntries comes back as 0.
Any ideas?
|
|
|
|
|
I had to do pretty much the same thing, but rather than use the Network management API, I used the osql.exe utility...
private void Form1_Load(object sender, System.EventArgs e)
{
System.Diagnostics.Process p;
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.FileName = "osql.exe";
psi.Arguments = "/L";
psi.CreateNoWindow = true;
p = new System.Diagnostics.Process(psi);
p.Start();
p.WaitForExit();
p.StandardOutput.ReadLine; // First line is blank
p.StandardOutput.ReadLine; // Second line reads 'Servers'
string s = p.StandardOutput.ReadToEnd();
System.Windows.Forms.MessageBox.Show(s);
}
Or in VB.NET...
Private Sub frmListServers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim prsOSQL As System.Diagnostics.Process
Dim psiOSQL As New System.Diagnostics.ProcessStartInfo
Dim srOSQL As System.IO.StreamReader
With psiOSQL
.RedirectStandardOutput = True
.CreateNoWindow = True
.UseShellExecute = False
.FileName = "osql.exe"
.Arguments = "/L"
End With
prsOSQL = System.Diagnostics.Process.Start(psiOSQL)
prsOSQL.WaitForExit()
srOSQL = prsOSQL.StandardOutput
srOSQL.ReadLine() ' Ignore the first (blank) line "
srOSQL.ReadLine() ' Ignore the second line - 'Servers:"
While srOSQL.Peek > 0
lstSQLServers.Items.Add(srOSQL.ReadLine.Trim)
End While
End Sub
Nak
|
|
|
|
|
This server combo box does not bring the domains on the network for windows 2003 Active Directory.
Plz help me out
|
|
|
|
|
I modified the combo box to print out the server types to the trace window in the following fashion:
foreach (String name in servers)<br />
{<br />
Trace.WriteLine( "serverType of " + name + ":" + Servers.GetServerType( name ) );<br />
Items.Add(name);<br />
}
The serve type of all the machines on my network is "None".
It appears that the problem is in Servers.GetServerType( string name ). The result value of NetServerGetInfo is zero on success but the function assumes that a result of zero is an error. Thus I changed this and it worked fine. My code for this function is now:
public static ServerType GetServerType(string serverName)<br />
{<br />
ServerType result = ServerType.None;<br />
<br />
IntPtr serverInfoPtr = IntPtr.Zero;<br />
uint rc = Win32API.NetServerGetInfo( serverName, 101, ref serverInfoPtr );<br />
if ( rc == 0 ) <br />
{<br />
Win32API.SERVER_INFO_101 si = (Win32API.SERVER_INFO_101) Marshal.PtrToStructure(serverInfoPtr, typeof(Win32API.SERVER_INFO_101));<br />
result = (ServerType) si.dwType;<br />
<br />
Win32API.NetApiBufferFree(serverInfoPtr);<br />
serverInfoPtr = IntPtr.Zero; <br />
}<br />
return result;<br />
}
Hope that helps.
-ben houston
|
|
|
|
|
it is very nice. and thanks for shareing it!
and how about to access the files on the severs in the lan?
i mean, could you read and write and copy or delete files on the servers in the lan?
that would be more nice?
|
|
|
|
|
Phil,
Thank you very much for an excellent piece of code. I had been searching for something like this off and on over the last week when I stumbled across yours.
I have placed it in my current project and had success except the following problem.
I am focusing on sensing SQL servers in my local network.
While I was testing the behavior of the control, I noticed that refreshing it would sense if a SQL server had went offline since the application had started, but if you restart the server, no matter how long you wait, refreshing does not sense that the server is back online.
In fact, if you are debugging, you have not only close the application but VS.NET before the control will sense that the server is back online!
Help?
|
|
|
|
|
With respect to ServerComboBox, you will notice the AutoRefresh property only will refresh when you change either the ServerType or DomainName properties. It does not poll for changes. (I am sure you could modify it to poll?). Explicitly calling Refresh() on the ServerComboBox should return the current visible servers/machines as per the ServerType property. Calling Refresh() will ultamately call Servers.GetEnumerator() which in turn calls the Win32 API function NetServerEnum().
When reviewing my code with respect to your problem, I did notice that I did not check the return code from NetServerEnum(). Perhaps that is my error? When using the debugger, did you try to set a break point at Servers.GetEnumerator() to ensure it is being called? If it is and NetServerEnum() is being called, but your not getting the results you expect... I would think something is wrong with NetServerEnum(). (You did say you were running under Windows Server 2003?).
Other than that, I have no suggestions. If you do indeed find a bug in this project, please let me know so that I can fix.
Phil
|
|
|
|
|
Anyone tried this with windows 2003 ? It works just fine in 2000 and XP, but no server/workstation/etc appears on the list with windows 2003
Cheers,
José Moura
|
|
|
|
|
Hi,
I've tried the component on Windows 2003 and it filled the box initially. But, my problem, which I'll address in another thread is that when I refresh, it senses a server being dropped but it does not sense when a new server is started.
|
|
|
|
|
Yea man!!! I love you! You have no idee how I wanted to enumerate computers on my LAN.
forever grateful, Andrei Matei
andreimatei@home.ro
|
|
|
|
|
Great Code indeed,
I am trying to use it in vb, but I am getting following error when I sent the server type property
For line
Me.sCombo.ServerType = NetworkManagement.ServerType.Server
i am getting
Value of type 'Integer' cannot be converted to 'NetworkManagement.ServerType'
I dont know how to do it, am i doing something wrong?
|
|
|
|
|
I have the same problem.
Did you manage to sort it out.
Thanks A lot
Gabriel
vbnetuk@yahoo.co.uk
|
|
|
|
|
I dont use VB, so I never looked at what the issue was. I developed this code when I was between projects. I have been really busy since then. Perhaps, I will run FxCop over the assembly to see if I coded something in non-standard .NET way. I have not had time to look at it.
Sorry
Phil
|
|
|
|
|
Your code is great but I cannot figure out why it doesnt work in vb.net
In vb.net we dont have uint but i dont think that's the problem.
Your dll would be of enormous help to me.
Thanks A lot
Gabriel
vbnetuk@yahoo.co.uk
|
|
|
|
|
Try changing line 56 of NetworkManagement.cs from:
public enum ServerType : uint
to
public enum ServerType : long
I am not currently on a network to test this change. It might work. I am casting elsewhere, so it shouldn't blow up. No other change should be required. I will not be able to test this change for a few weeks as my laptop is at home and I will not be in the office for that time. Good luck!
Phil
|
|
|
|
|
I am working on a version that will work in VB. I will post it on my site. Of course Phil will receive the majority of the credit.
Jason Gaylord
www.JasonGaylord.com
|
|
|
|
|
|
Why do we need to translate this code into VB? I thought the whole point of .NET was that different parts of a project could be written in different languages?
All you need to do is compile NetworkManagement.cs into a dll using the C# command-line compiler and then import it using the imports statement in VB...
|
|
|
|
|
You are correct about .NET, however, as I was not using a valid common language specification (CLS) data type for my enumeration and thus the code would not work under VB.
Section the C# Language Specification - 14.1 Enum declarations states:
"An enum declaration may explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long or ulong."
In the Visual Basic Language Specification - 7.4 Enumerations states:
"The underlying type of an enumeration must be an integral type that can represent all the enumerator values defined in the enumeration. If an underlying type is specified, it must be Byte, Short, Integer, or Long. If no underlying type is explicitly specified, the default is Integer."
And finally in .NET Framework Developer's Guide - Enumerations states:
"The underlying type must be one of the built-in signed or unsigned integer types (such as Byte, Int32, or UInt64)."
The section Introduction to the .NET Framework Developer's Guide - .NET Framework Class Library states Uint32 (VB) / unit (C#) type is "Not CLS-compliant".
As the assembly was written in C# and used from C#, it works. If the assembly was written in VB.NET and used from VB.NET, it should work (haven't tested). However when you try to mix the two, the problem a rises. (aka not using CLS compliant data types). Everything comes done to the interopability standard, and in this case it is the CLS.
So, I guess the bottom line, is if you want your code/assemblies to work across any .NET language, be sure that all publicly visible methods, enums, etc, use CLS compliant types.
Hopefully this clarifies the situation.
Phil Bolduc
|
|
|
|