public string fetchFileContent()
        {
            string              currentFileLine = "";
            string              totalLine       = "";
            StreamReader        sr   = new StreamReader(appath);
            ConnectHostWithPing Conn = new ConnectHostWithPing();

            while ((currentFileLine = sr.ReadLine()) != null)
            {
                totalLine += currentFileLine;
            }
            sr.Close();
            return(totalLine);
        }
        public ConnectHostWithPing generateHostList()
        {
            ConnectHostWithPing conn = new ConnectHostWithPing();

            conn.initIndex();
            string fileContent = fetchFileContent();

            string[] HostAndIPGroup = fileContent.Split(new string[] { "\t\t" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i <= HostAndIPGroup.Length - 1; i += 2)
            {
                conn.inesertHostAndIp(HostAndIPGroup[i], HostAndIPGroup[i + 1]);
            }
            return(conn);
        }
示例#3
0
        static void Main(string[] args)
        {
            FileOperation       file = new FileOperation();
            ConnectHostWithPing Conn = file.generateHostList();
            string header = string.Format("{0}\t{1}\t\t\t{2}", "Host名稱", "IPv4位址", "狀態");
            string Column = ""; string host = "";  string ip = "";  string command = "";

            Console.WriteLine(header);
            for (int i = 0; i <= Conn.getIpListLength() - 1; i++)
            {
                Column += Conn.connectInfo() + "\n";
                Conn.next();
            }
            Console.WriteLine(Column);

            while (true)
            {
                Column = "";
                Conn.initIndex();
                Console.WriteLine("\n Add Or Remove Or Ping \n");
                command = Console.ReadLine();
                if (command == "Add")
                {
                    Console.Write("Host:\t"); host = Console.ReadLine();
                    Console.Write("IP:\t");   ip   = Console.ReadLine();
                    Conn.inesertHostAndIp(host, ip);
                    FileOperation csv = new FileOperation();
                    csv.saveFile(Conn);
                }
                else if (command == "Remove")
                {
                    Console.Write("輸入欲刪除的IP:");
                    string removeIp = Console.ReadLine();
                    Conn.RemoveByIp(removeIp);
                    FileOperation csv = new FileOperation();
                    csv.saveFile(Conn);
                }
                else if (command == "Ping")
                {
                    for (int i = 0; i <= Conn.getIpListLength() - 1; i++)
                    {
                        Column += Conn.connectInfo() + "\n";
                        Conn.next();
                    }
                    Console.WriteLine(Column);
                }
            }
        }
        public void saveFile(ConnectHostWithPing data)
        {
            string hostList = "";

            for (int i = 0; i <= data.getIpListLength() - 1; i++)
            {
                hostList += data.makeHostList() + "\n";
                data.next();
            }
            try
            {
                FileStream   FileStream = new FileStream(appath, FileMode.Create);
                StreamWriter sw         = new StreamWriter(FileStream, System.Text.Encoding.UTF8);
                sw.WriteLine(hostList);
                sw.Flush();
                sw.Close();
                FileStream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }