0

I can't connect to wifi networks that the PC doesn't know. If I manually connect first, then the program is able to connect programmatically as long as I don't click on "Forget". If the network is not known then the ap.Connect(authRequest) returns null. How can I connect to a wifi network programmatically that the pc doesn't know yet?

var accessPoints = wifi.GetAccessPoints();
List<string> accessPointNames = new List<string>();
foreach (AccessPoint ap in accessPoints)
{
    accessPointNames.Add(ap.Name);
    string fSSID = "test1234";
    if (ap.Name == fSSID)
    {
        AuthRequest authRequest = new AuthRequest(ap)
        {
            Password = "12345678"
        };

        if (ap.Connect(authRequest))
            Console.WriteLine("connected");
        else
            Console.WriteLine("disconnected");
        break;
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
kreestyahn
  • 33
  • 2
  • Is `ap.Connect(authRequest))` really returning `null`, or is it causing an exception like `Value cannot be null. (Parameter 'stream')` as it is for me https://stackoverflow.com/questions/73364774/simple-wifi-bug-value-cannot-be-null-parameter-stream ? – Denis G. Labrecque Aug 16 '22 at 19:50
  • It's just null, not like "Value cannot be null. (Parameter 'stream')". – kreestyahn Sep 08 '22 at 08:42

1 Answers1

0

I had the exact same issue trying to use it in a Winforms application using .NET6.

Listing the networks, disconnecting from the network, and connecting to a network with a saved profile, all work perfectly. But overwriting a network with a profile, or trying to connect to a network that does not have a known profile throws the exception.

Running the standalone console app, works fine.

The error is there because it is not reading the template XML files. This is what I did to get it to work for me (the code probably could be simpler, but this is how I implemented it).

private static string GetTemplate(string name)
{
    string resourceName = string.Format("SimpleWifi.ProfileXML.{0}.xml", name);
    string result = string.Empty;
    var assembly = System.Reflection.Assembly.GetExecutingAssembly();
    using (var filestream = assembly.GetManifestResourceStream(resourceName))
    {
        byte[] fileContents = new byte[filestream.Length];
        filestream.Read(fileContents, 0, fileContents.Length);
        //
        using (MemoryStream stream = new MemoryStream(fileContents))
        {
            using (StreamReader sr = new StreamReader(stream))
            {
                result = sr.ReadToEnd();
            }
        }
    }
    return result;
}
DTrain
  • 21
  • 3