如何查找本地IP地址并将其保存为文本文件

本文关键字:保存 文件 文本 地址 IP 何查找 查找 | 更新日期: 2023-09-27 18:02:43

我编写了一个程序,它查找某人的本地IP地址并将输出保存为文本文件。但是我不能调用static void IP()方法,因为它需要string[] args参数我不知道怎么得到这个。我在哪里可以获得我需要传递给方法的数组?

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());
            }

        }
    }
}

如何查找本地IP地址并将其保存为文本文件

如果我正确理解了您的代码,您的意思是允许从命令行运行程序,并提供机器名称作为可选的命令行参数。

这是你的代码应该工作的版本:

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());
        }
    }
}
  • Main()方法被赋予一个string[] args参数,该参数将由运行时传递,并且将包含命令行参数。然后将其传递给IP()方法
  • 这是一个真的坏主意,以Console输出流写入文件。相反,请注意上面我是如何将writer对象直接传递给IP()方法,而不是绑定到Console类的。

您的代码运行良好,没有必要更改它。main方法可以接受字符串[]作为参数,然后将其传递给IP静态函数。

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(string[] args)
        {
            using (StreamWriter writer = new StreamWriter("E:''ip.txt"))
            {
                Console.SetOut(writer);
                 IP(args);
            }
        }
        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());
            }

        }
    }
}