SystemInformation::ComputerName Property
Gets the NetBIOS computer name of the local computer.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
The ComputerName property retrieves the NetBIOS name of the local computer. This is accomplished with a call into the native System Information function GetComputerName. This name is established at system startup, when the system reads it from the registry. If the local computer is a node in a cluster, ComputerName returns the name of the cluster virtual server.
You can use the ComputerName method to determine the name of the computer that is displayed to other users on a network.
The following code example lists all properties of the SystemInformation class in a ListBox and displays the current value of the property in a TextBox when a list item selected.
#using <System.Windows.Forms.dll> #using <System.Drawing.dll> #using <System.dll> using namespace System; using namespace System::Collections; using namespace System::ComponentModel; using namespace System::Drawing; using namespace System::Reflection; using namespace System::Windows::Forms; public ref class SystemInfoBrowserForm: public System::Windows::Forms::Form { private: System::Windows::Forms::ListBox^ listBox1; System::Windows::Forms::TextBox^ textBox1; public: SystemInfoBrowserForm() { this->SuspendLayout(); InitForm(); // Add each property of the SystemInformation class to the list box. Type^ t = System::Windows::Forms::SystemInformation::typeid; array<PropertyInfo^>^pi = t->GetProperties(); for ( int i = 0; i < pi->Length; i++ ) listBox1->Items->Add( pi[ i ]->Name ); textBox1->Text = String::Format( "The SystemInformation class has {0} properties.\r\n", pi->Length ); // Configure the list item selected handler for the list box to invoke a // method that displays the value of each property. listBox1->SelectedIndexChanged += gcnew EventHandler( this, &SystemInfoBrowserForm::listBox1_SelectedIndexChanged ); this->ResumeLayout( false ); } private: void listBox1_SelectedIndexChanged( Object^ /*sender*/, EventArgs^ /*e*/ ) { // Return if no list item is selected. if ( listBox1->SelectedIndex == -1 ) return; // Get the property name from the list item. String^ propname = listBox1->Text; if ( propname->Equals( "PowerStatus" ) ) { // Cycle and display the values of each property of the PowerStatus property. textBox1->Text = String::Concat( textBox1->Text, "\r\nThe value of the PowerStatus property is:" ); Type^ t = System::Windows::Forms::PowerStatus::typeid; array<PropertyInfo^>^pi = t->GetProperties(); for ( int i = 0; i < pi->Length; i++ ) { Object^ propval = pi[ i ]->GetValue( SystemInformation::PowerStatus, nullptr ); textBox1->Text = String::Format( "{0}\r\n PowerStatus.{1} is: {2}", textBox1->Text, pi[ i ]->Name, propval ); } } else { // Display the value of the selected property of the SystemInformation type. Type^ t = System::Windows::Forms::SystemInformation::typeid; array<PropertyInfo^>^pi = t->GetProperties(); PropertyInfo^ prop = nullptr; for ( int i = 0; i < pi->Length; i++ ) if ( pi[ i ]->Name == propname ) { prop = pi[ i ]; break; } Object^ propval = prop->GetValue( nullptr, nullptr ); textBox1->Text = String::Format( "{0}\r\nThe value of the {1} property is: {2}", textBox1->Text, propname, propval ); } } void InitForm() { // Initialize the form settings this->listBox1 = gcnew System::Windows::Forms::ListBox; this->textBox1 = gcnew System::Windows::Forms::TextBox; this->listBox1->Anchor = (System::Windows::Forms::AnchorStyles)(System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left | System::Windows::Forms::AnchorStyles::Right); this->listBox1->Location = System::Drawing::Point( 8, 16 ); this->listBox1->Size = System::Drawing::Size( 172, 496 ); this->listBox1->TabIndex = 0; this->textBox1->Anchor = (System::Windows::Forms::AnchorStyles)(System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right); this->textBox1->Location = System::Drawing::Point( 188, 16 ); this->textBox1->Multiline = true; this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical; this->textBox1->Size = System::Drawing::Size( 420, 496 ); this->textBox1->TabIndex = 1; this->ClientSize = System::Drawing::Size( 616, 525 ); this->Controls->Add( this->textBox1 ); this->Controls->Add( this->listBox1 ); this->Text = "Select a SystemInformation property to get the value of"; } }; [STAThread] int main() { Application::Run( gcnew SystemInfoBrowserForm ); }
- EnvironmentPermission
to get the value of this property. Associated enumeration: PermissionState::Unrestricted
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.