Solved

PC's full name

Posted on 2006-06-15
6
784 Views
Last Modified: 2008-02-01
Hi Guys,
               I need to retrieve the PC's full computer name, used to have an API function in VB6, any idea how to get it in C#??

Thanks
Serge
0
Comment
Question by:Autofreak
6 Comments
 
LVL 6

Assisted Solution

by:neilprice
neilprice earned 50 total points
ID: 16914154
Try this piece of code for an example;

You'll have to set the Properties->Build to allow unsafe code.

using System;
using System.Runtime.InteropServices;

class Class1
{
    [System.Runtime.InteropServices.DllImport("Kernel32")]
    static extern unsafe bool GetComputerName(byte* lpBuffer, long* nSize);

    [STAThread]
    static void Main(string[] args)
    {
        byte[] buffor = new byte[512];
        long size = buffor.Length;
        unsafe
        {
            long* pSize = &size;
            fixed (byte* pBuffor = buffor)
            {
                GetComputerName(pBuffor, pSize);
            }
        }
        System.Text.Encoding textEnc = new System.Text.ASCIIEncoding();
        System.Console.WriteLine("Computer name: {0}", textEnc.GetString(buffor));
        Console.ReadLine();
    }
}

Hope this helps,
Neil
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 150 total points
ID: 16914449
Have you tried:

string compName = Environment.MachineName;
0
 
LVL 6

Expert Comment

by:neilprice
ID: 16914984
Haha, that's a lot neater Zephyr :)

Neil
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:jvn222
ID: 16917517
FullName

MessageBox.Show(Environment.MachineName + "." Environment.UserDomainName);
0
 
LVL 4

Assisted Solution

by:jvn222
jvn222 earned 50 total points
ID: 16917519
sorry
MessageBox.Show(Environment.MachineName + "." + Environment.UserDomainName);
0
 

Author Comment

by:Autofreak
ID: 16921465
Man, Is C# neat !? Love IT!
Really appreciate your solutions,
Serge
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now