Work code assistance

namespace IPScannerByNR
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn ipAddressColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn statusColumn;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox ipAddressTextBox;
        private System.Windows.Forms.Button setIpButton;

        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.ipAddressColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.statusColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.button1 = new System.Windows.Forms.Button();
            this.ipAddressTextBox = new System.Windows.Forms.TextBox();
            this.setIpButton = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.ipAddressColumn,
            this.statusColumn});
            this.dataGridView1.Location = new System.Drawing.Point(16, 15);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowHeadersWidth = 51;
            this.dataGridView1.Size = new System.Drawing.Size(1035, 487);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
            // 
            // IPAddressColumn
            // 
            this.ipAddressColumn.HeaderText = "IP Address";
            this.ipAddressColumn.MinimumWidth = 6;
            this.ipAddressColumn.Name = "ipAddressColumn";
            this.ipAddressColumn.ReadOnly = true;
            this.ipAddressColumn.Width = 125;
            // 
            // statusColumn
            // 
            this.statusColumn.HeaderText = "Status";
            this.statusColumn.MinimumWidth = 6;
            this.statusColumn.Name = "statusColumn";
            this.statusColumn.ReadOnly = true;
            this.statusColumn.Width = 125;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(951, 510);
            this.button1.Margin = new System.Windows.Forms.Padding(4);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(100, 28);
            this.button1.TabIndex = 1;
            this.button1.Text = "Start Ping";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);

            //
            // ipAddressTextBox
            //
            this.ipAddressTextBox.Location = new System.Drawing.Point(16, 510);
            this.ipAddressTextBox.Margin = new System.Windows.Forms.Padding(4);
            this.ipAddressTextBox.Name = "ipAddressTextBox";
            this.ipAddressTextBox.Size = new System.Drawing.Size(200, 22);
            this.ipAddressTextBox.TabIndex = 2;

            //
            // setIpButton
            //
            this.setIpButton.Location = new System.Drawing.Point(224, 510);
            this.setIpButton.Margin = new System.Windows.Forms.Padding(4);
            this.setIpButton.Name = "setIpButton";
            this.setIpButton.Size = new System.Drawing.Size(100, 28);
            this.setIpButton.TabIndex = 3;
            this.setIpButton.Text = "Set IP";
            this.setIpButton.Click += new System.EventHandler(this.setIpButton_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1067, 554);
            this.Controls.Add(this.setIpButton);
            this.Controls.Add(this.ipAddressTextBox);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.dataGridView1);
            this.Margin = new System.Windows.Forms.Padding(4);
            this.Name = "Form1";
            this.Text = "IP Scanner";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}


using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IPScannerByNR
{
    public partial class Form1 : Form
    {
        private string _ipAddress = "1.51.98";
        private bool isRunning = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isRunning)
            {
                MessageBox.Show("Operation already in progress.");
                return;
            }

            isRunning = true;
            button1.Enabled = false;
            dataGridView1.Rows.Clear();

            Task.Run(() => RunPingScript()).ContinueWith(t =>
            {
                this.Invoke((Action)(() =>
                {
                    button1.Enabled = true;
                    isRunning = false;
                }));
            });
        }

        private void setIpButton_Click(object sender, EventArgs e)
        {
            string ipAddressInput = ipAddressTextBox.Text;

            System.Net.IPAddress ipAddress;

            if (string.IsNullOrEmpty(ipAddressInput) || !System.Net.IPAddress.TryParse(ipAddressInput, out ipAddress))
            {
                MessageBox.Show("Please Enter a Valid IP address.", "Invalid IP", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _ipAddress = ipAddressInput;

            dataGridView1.Rows.Clear();
        }

        private void RunPingScript()
        {
            string batchFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "pingScript.bat");

            string scriptContent = "@Echo off\n" +
            "setlocal enabledelayedexpansion\n" +
            "for /L %%i in (2,1,254) do (\n" +
            "    set \"status=In Use\"\n" +
            "    ping -4 -n 1 -w 3000 " + _ipAddress + ".%%i > tempPingResult.txt\n" +
            "    for /f \"tokens=* delims=\" %%a in (tempPingResult.txt) do (\n" +
            "        echo %%a | find /i \"Request timed out.\" > nul && set \"status=Request Timed out\"\n" +
            "        echo %%a | find /i \"Destination host unreachable.\" > nul && set \"status=Destination host unreachable\"\n" +
            "        echo %%a | find /i \"TTL=\" > nul && set \"status=In Use\"\n" +
            "    )\n" +
            "    echo " + _ipAddress + ".%%i !status!\n" +
            ")\n" +
            "del tempPingResult.txt";

            File.WriteAllText(batchFilePath, scriptContent);

            ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/c " + batchFilePath)
            {
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            using (Process process = new Process())
            {
                process.StartInfo = processInfo;

                process.OutputDataReceived += (sender, args) =>
                {
                    if (args.Data != null)
                    {
                        this.BeginInvoke((Action)(() =>
                        {
                            if (!this.IsDisposed && this.IsHandleCreated)
                            {
                                UpdateDataGridView(args.Data);
                            }
                        }));
                    }
                };

                process.ErrorDataReceived += (sender, args) =>
                {
                    if (args.Data != null)
                    {
                        Console.Error.WriteLine("Error: " + args.Data);
                    }
                };

                try
                {
                    process.Start();
                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();
                    process.WaitForExit();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Exception occurred: " + ex.Message);
                }
            }
        }

        private void UpdateDataGridView(string output)
        {

            string[] parts = output.Split(new[] { ' ' }, 2);

            if (parts.Length > 1)
            {

                string ip = parts[0];

                string status = parts[1].Trim();



                if (ip == _ipAddress + ".1" && status == "Unknown")
                {

                    status = "In Use";

                }

                int rowIndex = dataGridView1.Rows.Add(ip, status);



                switch (status)
                {

                    case "Request Timed out":

                        dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Red;

                        break;

                    case "Destination host unreachable":

                        dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Green; // Use a different color if needed

                        break;

                    case "In Use":

                        dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;

                        break;

                    default:

                        dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Gray;

                        break;

                }

                Console.WriteLine("Added IP: " + ip + " Status: " + status);

            }

            else
            {

                Console.WriteLine("Output: " + output);

            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                string ipAddress = row.Cells["ipAddressColumn"].Value.ToString();
                string status = row.Cells["statusColumn"].Value.ToString();
                MessageBox.Show("IP Address: " + ipAddress + "\nStatus: " + status + " Cell Clicked");
            }
        }
    }
}