0

I am working on a project, where I use netsh to get all wlan networks and later get the keys. At first I had 3 applications and 2 .txt files, where I transfered information between those appications. Now I succsessfully copied all 3 applications into 1, while stil using those text files. Now I want to get rid of those files to have an standalone exe.

To the actual problem: In one part of the code I open a netsh.exe window and using streamwriter write some commands into it. After that I use streamreader to read everything and print it ino one of the txt files. After that I read the contents of the file, while skipping a few lines and printing the rest into a list box. Is there a way to do all of that without using a text file?

Heres some code:

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();


            var p = new Process();
            var startinfo = new ProcessStartInfo("cmd.exe")
            {
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false
            };
            p.StartInfo = startinfo;
            p.Start();

            using (StreamWriter sw = p.StandardInput)
            {
                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine("@echo off");
                    sw.WriteLine("netsh wlan show profiles");
                }
            }
            using (StreamReader sr = p.StandardOutput)
            {
                if (sr.BaseStream.CanRead)
                {
                    
                }
            }
        }

The output would be:

Profile auf Schnittstelle WLAN:

Gruppenrichtlinienprofile (schreibgeschützt)
---------------------------------
    <Kein>

Benutzerprofile
---------------
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
    Profil für alle Benutzer : something
  • [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103) – Jimi Jun 28 '22 at 10:49

1 Answers1

0

Use MemoryStream instead of FileStream

SuRGeoNix
  • 482
  • 1
  • 3
  • 10