Reputation: 12397
The MSDN documentation for ServiceController.GetServices stipulates that the function will throw an ArgumentException
when
The machineName parameter has invalid syntax.
How can I validate a machine name for valid syntax? Is it similar to Wikipedia's Restrictions on valid host names? I imagine that it would have to cover at least that, but does Windows put any further restrictions on what's valid?
Upvotes: 1
Views: 400
Reputation: 69
This is actually not as easy to answer as it seems. The problem is that a machine name often is used unchanged for identification in other services. So its possible to just test its validity as a name for the machine (alphanumeric characters only, no control character, no spaces, no extended charset) but depending on your solution you may want to validate that it also functions as a valid DNS name and perhaps some other services?
I suggest you test both for validity as a NetBIOS and DNS name, here is a good page that describes the rules for both http://support.microsoft.com/kb/909264.
Upvotes: 1
Reputation: 21
.Net has an API to verify whether the specified host name is a valid DNS name. http://msdn.microsoft.com/en-us/library/system.uri.checkhostname(v=vs.110).aspx which could be a good run-time check. Use this then you won't have to create a parser for the machine name looking for illegal chars or strlen restrictions.
Upvotes: 1
Reputation: 11
Have a look at this: http://support.microsoft.com/kb/909264
Though it mentions Active Directory, I believe the NetBIOS names section applies to all scenarios.
Hope this helps.
Sri
Upvotes: 1