2
static void f_WriteIpAddresses()
{
    IPHostEntry host;
    string localIP = "?";
    host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        Console.Write(ip.ToString());
        Console.Write(" - ");
    }
}

static void Main(string[] args)
{
    Console.WriteLine("Your IP Address:")
    WriteIpAddresses();
    Console.Write("[You can delete/modify address]");
    string ip = Console.ReadLine();
}

I want to find all address and write on the line where user can delete until the right address or modify the line.

c:\>address.exe
Your IP Address:
192.168.1.13 - 10.10.2.15 [You can delete/modify address]
uzay95
  • 16,052
  • 31
  • 116
  • 182

2 Answers2

4

Check out this post, it explains how to clear parts of the console. This way you can also set the console-cursor at a place where you want so you can let the user overwrite certain parts in the console. See c-sharp-console-console-clear-problem

This post could also be quite handy, quite similar problem.

Community
  • 1
  • 1
Brian
  • 1,803
  • 1
  • 16
  • 22
2

You could do something like putting a \r at the end of the last console.write to get the cursor at the beginning of the line.

Then read your input and combine the new input with the original output. Since your readline is only reading the new data, you must add (overwrite) this new data in your original data.

However this will only work if the printed line length is smaller than the console width.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ConsoleApplication3
{
    class Program
    {
        static string f_WriteIpAddresses()
        {
            IPHostEntry host;
            string localIP = "?";
            string retVal = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    Console.Write(ip.ToString());
                    Console.Write(" - ");
                    retVal = ip.ToString() + " - "; 
                }
            }
            return retVal;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Your IP Address:");
            string oldip = f_WriteIpAddresses();
            Console.Write("[You can delete/modify address]\r");
            string ip = Console.ReadLine();

            string newip = ip + oldip.Substring(ip.Length);
        }
    }
}

The above should give a new ip address but has limitations such as, only works for one single line etc.

bart s
  • 5,068
  • 1
  • 34
  • 55
  • This method works fantastic for what I would call a temporary message in the console output, works in command line scripts too. just do a Console.Write + Msg + \r (or vbcr for VBS) then your next console.writeline will overwrite your last temporary message. – DarrenMB Jun 03 '20 at 13:43