Hi,
I am use following code to find IP address of machine in C#.write following code in class file.
when build the solution it gives following error
1.The best overloaded method match for 'string.String(char*)' has some invalid arguments
2.Argument '1':cannot convert from 'string' to 'char*'
How to solve this error and access this class file in form

public static int MainIP(string [] args)
        {
            String strHostName = new String("");

            if (args.Length == 0)
            {
              
                strHostName = Dns.GetHostName();
                Console.WriteLine("Local Machine Name :" + strHostName);
            }
            else
            {
                strHostName = args[0];
            }

            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
            }
            return 0;
        }

Dani AI

Generated

— the compiler error comes from new String(""). In C# string (System.String) doesn't have a constructor that takes another string, so new String("") can't be compiled. Replace that with a literal or String.Empty (for example string host = String.Empty;) or just declare string host; and assign Dns.GetHostName() later. Also, public static int MainIP(string[] args) looks like a console Main signature — for use from a WinForms button make the method a descriptive helper (returning a list of addresses) instead of returning an int.

The approach in your second post (using DNS APIs) is fine for a quick check, but Dns.GetHostEntry can return IPv6 addresses, loopback entries, or addresses for virtual adapters. For a robust local IPv4 result iterate network interfaces and select the first operational, non-loopback IPv4 address. Handle SocketException when using DNS resolution, and remember: if you need the public IP (the one seen on the Internet) you must query an external service — local APIs only show local network addresses.

Example helper (get non-loopback IPv4 addresses):

using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Collections.Generic;

public static List<IPAddress> GetLocalIPv4Addresses()
{
    var list = new List<IPAddress>();
    foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (ni.OperationalStatus != OperationalStatus.Up) continue;
        foreach (var ua in ni.GetIPProperties().UnicastAddresses)
        {
            if (ua.Address.AddressFamily == AddressFamily.InterNetwork &&
                !IPAddress.IsLoopback(ua.Address))
            {
                list.Add(ua.Address);
            }
        }
    }
    return list;
}

Call that from your form button, iterate the returned list and display or use the first entry. Quick tips: use Environment.MachineName when you just need the machine name; filter by AddressFamily.InterNetwork for IPv4; and catch exceptions around DNS/network calls.

The code for find IP address and machine name in Windows Programming

private void button1_Click(object sender, EventArgs e)
        {
            
            string myHost = System.Net.Dns.GetHostName();

            System.Net.IPHostEntry myIPs = System.Net.Dns.GetHostEntry(myHost);

            // Loop through all IP addresses and display each 

            foreach (System.Net.IPAddress myIP in myIPs.AddressList)
            {

                MessageBox.Show(myIP.ToString());

            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.