Max Titox T Max Titox T - 3 months ago 8
C# Question

How to look up a local IP address and save it to a text file

I made a program which looks up someone's local IP address and saves the output as a text file. But I can't call the

static void IP()
method, because it needs the the
string[] args
parameter and I don't know how to get this. Where can I obtain the array I need to pass to the method?

using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IPObtainer
{
class Program
{
static void Main()
{
using (StreamWriter writer = new StreamWriter("E:\\ip.txt"))
{
Console.SetOut(writer);
IP();
}
}

static void IP(string[] args)
{
String StringHost;
if (args.Length == 0)
{
// Getting IP of local machine...
// First get the host name of the local machine...
StringHost = System.Net.Dns.GetHostName();
Console.WriteLine("Local machine host name is: " + StringHost);
Console.WriteLine("");

}
else
{
StringHost = args[0];
}


// The using hostname, get the IP address List...
IPHostEntry ipEntry =
System.Net.Dns.GetHostEntry(StringHost);

IPAddress[] address = ipEntry.AddressList;

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


}
}
}

Answer

If I understand the code you've correctly, you mean to allow the program to be run from the command line, with a machine name provided as an optional command line argument.

Here is a version of your code that should work:

class Program
{
    static void Main(string[] args)
    {
        using (StreamWriter writer = new StreamWriter("E:\\ip.txt"))
        {
            IP(args, writer);
        }
    }

    static void IP(string[] args, TextWriter writer)
    {
        String StringHost;
        if (args.Length == 0)
        {
            // Getting IP of local machine...
            // First get the host name of the local machine...
            StringHost = System.Net.Dns.GetHostName();
            writer.WriteLine("Local machine host name is: " + StringHost);
            writer.WriteLine("");

        }
        else
        {
            StringHost = args[0];
        }

        // The using hostname, get the IP address List...
        IPHostEntry ipEntry =
            System.Net.Dns.GetHostEntry(StringHost);

        IPAddress[] address = ipEntry.AddressList;

        for (int i = 0; i < address.Length; i++)
        {
            writer.WriteLine("");
            writer.WriteLine("IP Address Type {0}: {1} ", i, address[i].ToString());
        }
    }
}
  • The Main() method is given a string[] args parameter, which will be passed by the runtime, and will contain the command line arguments. This is then passed to your IP() method
  • It is a really bad idea to co-opt the Console output streams for writing to a file. Instead, note in the above how I pass the writer object to the IP() method and let it us that directly, instead of being tied to the Console class.