0

Is there a safe way to remove the domain name from a machine name in C#?

Example:

MYMACHINE.mydomain.com

should be resolved to

MYMACHINE

Antineutrino
  • 1,093
  • 3
  • 10
  • 26

2 Answers2

3

Naive String Approach:

string fullName = "MYMACHINE.mydomain.com";
string resolvedMachineName = fullName.Split('.')[0];
James
  • 2,445
  • 2
  • 25
  • 35
  • So, I have to parse strings to find out? – Antineutrino Aug 01 '12 at 13:38
  • @Antineutrino I assumed you already had the full name stored as a string, how do you have it stored? – James Aug 01 '12 at 13:39
  • It's worth noting that this approach assumes you'll always have a three part path. If the domain is something like MYMACHINE.mydomain.co.uk it will break your logic. – StoriKnow Aug 01 '12 at 13:39
  • 4
    @Sam How so? It grabs the first element in a string of period delimited elements. It's safe to directly index the first element as split will always contain at least one element – James Aug 01 '12 at 13:41
  • @James: I only have the string with the domain name as suffix. – Antineutrino Aug 01 '12 at 13:44
  • @sam it wont break the logic, +1 – Les Aug 01 '12 at 13:52
  • @James oops. no redeeming myself on that one. sorry for the incorrect info Antineutrino and thanks for pointing it out James (and Les). – StoriKnow Aug 01 '12 at 13:54
2

If you want the 'basic' understanding of machine name, then use :

Environment.MachineName

It provides the machine name without the domain (traditionally the COMPUTERNAME variable at a command prompt). See: http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx


Edit #1
@Antineutrino - as I re-read my answer I realized we don't have enough information to give you a correct answer. When you say 'machine name', however, it is not clear if you want to know how to parse the string you have, or the proper way to retrieve what you want. If you simply want to know how to parse a string, the other answers are correct. On the other hand...

  • Do you want the NetBios name or Cluster Name? Then Environment.MachineName

  • Do you want the underlying name of a the machine running a cluster? You'll need to dig into the WMI classes.

  • Do you want the most specific name from all DNS strings that resolve to the current machine? i.e. web1 from web1.mysite.com? You'll need to resolve all the IP's bound to the machine.

If you go back and edit/revise your question with more details -- you'll get better/more-specific answers.


Edit #2
If I understand correctly, your scenario is as follows: You have similar code that runs on both client and server machines in a desktop environment. In all machines registries you've saved a DNS name for the server (server.myapp.com). Now in the code you want to determine if a machine is the server or not.

What You Asked For

string serverName     = Registry.GetValue(REG_KEY, REG_VALUE, "key missing");
string currentMachine = Environment.MachineName;

if (currentMachine.ToLower()  == serverName.ToLower().Split('.')[0] ) {
    //do something fancy
};

Another Idea
Instead of shrinking the server name, you can grow the machine name.

string serverName     = Registry.GetValue(REG_KEY, REG_VALUE, "key missing");
string currentMachine = Environment.MachineName + 
                         Environment.GetEnvironmentVariable("USERDNSDOMAIN");

if (currentMachine.ToLower()  == serverName.ToLower() ) {
    //do something fancy
};

What you may want

Distributing registry keys seems a bit cumbersome. If it were me I would probably do one/some/many of the following:

  1. Create different build configurations and use pre-processor directives to change the code. Something very roughly like: #if(RELEASE_SERVER) { /*do server code*/} #if(RELEASE_CLIENT) { /*do server code*/}

  2. Use app.config ---that's what it's there to store configuration data. No need to pollute the registry. app.config can be transformed on build/deployment, or simply modified manually on the one server.

  3. Store the majority of the configuration data in a central location. This way the app only need to know were to retrieve it's configuration from (even if that's itself).

Community
  • 1
  • 1
EBarr
  • 11,826
  • 7
  • 63
  • 85
  • 1
    +1 For trying to solve what the OP needs, not what he thinks he wants – James Aug 01 '12 at 13:57
  • I often find myself wishing that people would do/help with what i want, instead of what i ask:-D – EBarr Aug 01 '12 at 13:58
  • @EBar: I want to have the value that is returned by Environment.MachineName. – Antineutrino Aug 01 '12 at 14:00
  • I have stored something like SOMEMACHINE.mydomain.com as the server name in the windows registry. Now I have to compare this value with Environment.MachineName to check if the code is running on the application server and not on the client. This obviously doesn't work if SOMEMACHINE and SOMEMACHINE.domain.com is compared as strings. So I have to extract SOMEMACHINE out of SOMEMACHINE.domain.com. – Antineutrino Aug 01 '12 at 14:23
  • I'll modify my answer to add an idea or two. It bears repeating -- spending a few more minutes writing up the questions (or editing it) with details like will get you better answers -- and avoid the need for the all comment back-and-forth. – EBarr Aug 01 '12 at 14:44