Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want the 'basic' understanding of machine name, then use :</p> <pre><code>Environment.MachineName </code></pre> <p>It provides the machine name without the domain (traditionally the <code>COMPUTERNAME</code> variable at a command prompt). See: <a href="http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.environment.machinename.aspx</a></p> <hr> <p><strong>Edit #1</strong><br> @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...</p> <ul> <li><p>Do you want the NetBios name or Cluster Name? Then <code>Environment.MachineName</code> </p></li> <li><p>Do you want the underlying name of a the machine running a cluster? You'll need to <a href="http://learningpcs.blogspot.com/2009/09/wmic-get-active-cluster-machine-name.html" rel="nofollow noreferrer">dig into the WMI classes</a>. </p></li> <li><p>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 <a href="https://stackoverflow.com/questions/5271724/get-all-ip-addresses-on-machine">resolve all the IP's bound to the machine</a>.</p></li> </ul> <p>If you go back and edit/revise your question with more details -- you'll get better/more-specific answers.</p> <hr> <p><strong>Edit #2</strong><br> 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. </p> <p><em>What You Asked For</em> </p> <pre><code>string serverName = Registry.GetValue(REG_KEY, REG_VALUE, "key missing"); string currentMachine = Environment.MachineName; if (currentMachine.ToLower() == serverName.ToLower().Split('.')[0] ) { //do something fancy }; </code></pre> <p><em>Another Idea</em><br> Instead of shrinking the server name, you can grow the machine name.</p> <pre><code>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 }; </code></pre> <p><em>What you <strong>may</strong> want</em> </p> <p>Distributing registry keys seems a bit cumbersome. If it were me I would probably do one/some/many of the following:</p> <ol> <li><p>Create different build configurations and use <a href="http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx" rel="nofollow noreferrer">pre-processor directives</a> to change the code. Something very roughly like: <code>#if(RELEASE_SERVER) { /*do server code*/} #if(RELEASE_CLIENT) { /*do server code*/}</code></p></li> <li><p>Use app.config ---that's what it's there to store configuration data. No need to pollute the registry. app.config <a href="https://stackoverflow.com/questions/3004210">can be transformed on build/deployment</a>, or simply modified manually on the one server.</p></li> <li><p>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).</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload