Ejemplo n.º 1
0
        private async Task <object> LoadTxt(string ipaddr, string subnet)
        {
            return(await Task.Run((Func <List <IPAndName> >)(() =>
            {
                List <IPAndName> list = new List <IPAndName>();
                try
                {
                    ProcessStartInfo runNBTScan = new ProcessStartInfo();
                    runNBTScan.FileName = "cmd.exe";
                    string fileName = System.IO.Path.Combine(Environment.CurrentDirectory, $"{ipaddr}List.txt");
                    string nbtscan = System.IO.Path.Combine(Environment.CurrentDirectory, @"SupportTools\nbtscan.exe");
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    runNBTScan.Arguments = $"/c start /b {nbtscan} -s : {ipaddr}/{subnet} >{fileName}";

                    //Process.Start(runNBTScan);
                    Process proc = new Process();
                    proc.StartInfo = runNBTScan;
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    proc.Start();

                    bool startStream = false;
                    while (!startStream)
                    {
                        try
                        {
                            using (StreamReader str1 = new StreamReader(fileName)) { };
                            startStream = true;
                        }
                        catch (Exception)
                        {
                        }
                    }



                    using (StreamReader str = new StreamReader(fileName))
                    {
                        while (!str.EndOfStream)
                        {
                            string input = str.ReadLine();
                            string paternIP = @"([0-9]{1,3}[\.]){3}[0-9]{1,3}";
                            Match matchIP = Regex.Match(input, paternIP);
                            if (matchIP != null)
                            {
                                input = input.Replace(" ", "");
                                string[] workgroupAndName = input.Split(new char[] { ':' });
                                IPAndName curIPN = new IPAndName(matchIP.Value, "", workgroupAndName[1], workgroupAndName[4]);
                                list.Add(curIPN);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Bindings.StatusBarText = ex.Message;
                }

                return list;
            })));
        }
Ejemplo n.º 2
0
        public void CreateListIPNameFromFile()
        {
            try
            {
                string fileName = System.IO.Path.Combine(Environment.CurrentDirectory, $"172.16.5.0List.txt");

                List <IPAndName> list = new List <IPAndName>();

                using (StreamReader str = new StreamReader(fileName))
                {
                    while (!str.EndOfStream)
                    {
                        string input    = str.ReadLine();
                        string paternIP = @"([0-9]{1,3}[\.]){3}[0-9]{1,3}";
                        Match  matchIP  = Regex.Match(input, paternIP);
                        if (matchIP != null)
                        {
                            input = input.Replace(" ", "");
                            string[]  workgroupAndName = input.Split(new char[] { ':' });
                            IPAndName curIPN           = new IPAndName(matchIP.Value, "", workgroupAndName[1], workgroupAndName[4]);
                            list.Add(curIPN);
                        }
                    }
                }
                foreach (IPAndName item in list)
                {
                    if ((from i in listPcIPAndPcName
                         where i.PcIP.ToLower() == item.PcIP.ToLower() && i.PcName.ToLower() == item.PcName.ToLower()
                         select i).FirstOrDefault() == null)
                    {
                        listPcIPAndPcName.Add(item);
                    }
                }


                //object x = await LoadTxt("172.16.11.0", "24");
                fileName = System.IO.Path.Combine(Environment.CurrentDirectory, $"172.16.11.0List.txt");

                list = new List <IPAndName>();

                using (StreamReader str = new StreamReader(fileName))
                {
                    while (!str.EndOfStream)
                    {
                        string input    = str.ReadLine();
                        string paternIP = @"([0-9]{1,3}[\.]){3}[0-9]{1,3}";
                        Match  matchIP  = Regex.Match(input, paternIP);
                        if (matchIP != null)
                        {
                            input = input.Replace(" ", "");
                            string[]  workgroupAndName = input.Split(new char[] { ':' });
                            IPAndName curIPN           = new IPAndName(matchIP.Value, "", workgroupAndName[1], workgroupAndName[4]);
                            list.Add(curIPN);
                        }
                    }
                }
                foreach (IPAndName item in list)
                {
                    if ((from i in listPcIPAndPcName
                         where i.PcIP.ToLower() == item.PcIP.ToLower() && i.PcName.ToLower() == item.PcName.ToLower()
                         select i).FirstOrDefault() == null)
                    {
                        listPcIPAndPcName.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                Bindings.StatusBarText = ex.Message;
            }
        }