0

I have to store each profile name in an Observable collection, but I don't know how to do this, I made a big part of the project, but it is how to acces to EACH profile name that I don't know how to do.

I saw people are using Substrings and IndexOf, I tried but the problem is that I have more than only one profile name to display so this isn't working.

I followed this tutorial: https://www.youtube.com/watch?v=Yr3nfHiA8Kk But it is showing how to do but with the Wifi currently connected

InitializeComponent();
            ObservableCollection<String> reseaux = new ObservableCollection<String>();

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "netsh.exe";
            //p.StartInfo.Arguments = "wlan show interfaces";
            p.StartInfo.Arguments = "wlan show profile";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();

        /*foreach (System.Diagnostics.Process profile in profile)
        {
            reseaux.Add(reseauName);
        }*/

        lesReseaux.ItemsSource = reseaux;
Dolotboy
  • 74
  • 7
  • The Process class do not have any environmental variables like PATH. So you need to full path name of netsh.exe. – jdweng Oct 15 '20 at 17:09
  • It is working, my project is able to find netsh.exe – Dolotboy Oct 15 '20 at 17:15
  • Isn't the argument to `show` supposed to be `profiles` (with an `s`)? When I open a command window and type `netsh.exe` followed by `wlan show /?`, I don't see a `profile` option. – Rufus L Oct 15 '20 at 17:15
  • Also, since you're redirecting the output, shouldn't you be getting the data from `p.StandardOutput`? – Rufus L Oct 15 '20 at 17:16
  • Please narrow the question down to a *specific* problem. What is the exact issue you're running into? Are you getting an error? Incorrect results? An exception? – Rufus L Oct 15 '20 at 17:17
  • There is no error yet, I simply don't know how to get every profile name in a List, Here is every profile name that I have https://ibb.co/gvhD2PH, There should be a way to store them in a List – Dolotboy Oct 15 '20 at 17:21
  • 1
    See msdn for reading standardoutput (https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=netcore-3.1). It is a stream and you cannot enumerate through the stream or a string. The output is probably multiple lines and you need to split on the end of line terminator. – jdweng Oct 15 '20 at 17:22
  • Alright, thanks for your advice i'll continue this way :) This is all I needed a way to follow to be able to continue in my project – Dolotboy Oct 15 '20 at 17:26

1 Answers1

1

I don't have a way to test this at the moment, but based on the output you've shown in the image it appears that you could take all the output, split it into individual lines, split each line on the ':' character, and then select the second part from that split to get the name.

But first, I think the argument to show is "profiles" (plural), and according to one of the comments you may need to use the full path to netsh.exe. So that code might look like:

var startInfo = new ProcessStartInfo
{
    FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
    Arguments = "wlan show profiles",
    UseShellExecute = false,
    RedirectStandardOutput = true,
};

var p = Process.Start(startInfo);
p.WaitForExit();

After this, the output from the command will be stored in p.StandardOutput (which is a StreamReader), and we can get it all as a string using .ReadToEnd():

var output = p.StandardOutput
    // Get all the output
    .ReadToEnd()
    // Split it into lines
    .Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    // Split each line on the ':' character
    .Select(line => line.Split(new[] {':'}, StringSplitOptions.RemoveEmptyEntries))
    // Get only lines that have something after the ':'
    .Where(split => split.Length > 1)
    // Select and trim the value after the ':'
    .Select(split => split[1].Trim());

Now that we have an IEnumerable<string> of the names, we can initialize our collection with it:

var reseaux = new ObservableCollection<string>(output);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • OMG Thanks i wasn't thinking that way i was sure i needed to find a file that had this info in and i had to go and extract it to put it in my list so if i understand your code correctly you created a list and avex put each shelled row in it? – Dolotboy Oct 15 '20 at 19:03
  • 1
    No, I tried to describe it in the comments. It takes all the output into a single string, then splits that into an array of lines, and then for any line that has a `':'` with something after it, it takes the part after that character and returns them all in a list. So it *should* be a list of profile names. Let me know if it works, cause I can't test it on my phone – Rufus L Oct 15 '20 at 19:41