-4

How can I get a part of a system's name? For instance if the computer name contains SERVER then do something, and if it includes CLIENT then do something else.

The names I an working with are similar to SERVER000455. The numerical values change all the time but the SERVER part is the same.

Getting the name is easy (SystemInformation.ComputerName), but that wasn't my question. My question was getting just the first part of the name. The string[] and split doesn't seem to work since it's not an array. I just need to get the prefix of the PC name and drop the numerical bit.

gunr2171
  • 10,315
  • 25
  • 52
  • 75
ShaneTheTech
  • 127
  • 1
  • 2
  • 10
  • Can we see the error that you get? – Bob. Jun 11 '13 at 20:35
  • wow, thanks Jonesy for nothing. getting the name is easy SystemInformation.ComputerName that wasn't my question, my question was getting just the first part of the name. the srting[] and split doesn't seem to work since it's not an array. I just need to get the prefix of the PC name and drop the numerical bit – ShaneTheTech Jun 11 '13 at 20:37
  • See this question: [how do I get the local machine name?](http://stackoverflow.com/questions/662282/how-do-i-get-the-local-machine-name) – jszigeti Jun 11 '13 at 20:38
  • 1
    Well, then, why did you need to ask the question? – Kendall Frey Jun 11 '13 at 20:38
  • @ShaneTheTech my smartass-ness aside, it was not the answer you wanted, but the answer you needed sir. – Jonesopolis Jun 11 '13 at 20:42
  • This question does have a big problem. You don't show what you tried. Thus people got confused, and didn't know what you were even trying to do. – Kendall Frey Jun 11 '13 at 20:46
  • I guess I could have explained better, but the basic question was how to get PART of a computers name, not the name itself. I thought giving the basic examples of this is the names in general IE: SERVER000455 and CLIENT000455, but all i needed was the name not the numerical values. – ShaneTheTech Jun 11 '13 at 20:50
  • I do thank everyone on this site for assisting me on this learning process with C# and I hope that soon I will be able to help others as well in other areas. I am learning from a book and in VS 2012....it's not been so easy as of yet but I am getting there. – ShaneTheTech Jun 11 '13 at 20:52

2 Answers2

3

Okay, I thought the main question was the computer name. But you want:

String machinename = System.Environment.MachineName;

if(machinename.ToUpper().Contains("SERVER"))
{

}
else if (machinename.ToUpper().Contains("CLIENT"))
{

}
Jonesopolis
  • 23,589
  • 10
  • 63
  • 106
2
if (Environment.MachineName.ToUpper().Contains("SERVER"))
{
    // etc.
}
Kendall Frey
  • 39,334
  • 18
  • 104
  • 142