우선 netsh.exe 를 실행시키는 함수를 구성합니다.
private void StartNetsh(string args)
{
Process p = new Process();
p.StartInfo.FileName = "netsh.exe";
p.StartInfo.Arguments = args;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
}
기존 와이파이를 해지합니다.
//기존의 연결을 해지한다
StartNetsh($"wlan disconnect");
이전에 쓰던 와이파이 프로필을 삭제합니다. (옵셔널)
//새로운 프로필을 추가하기 전에 이전에 쓰던 프로필을 삭제시킨다.
StartNetsh($"wlan delete profile name=\"{PREVIOUS_HMD_WIFI_NAME}\"");
윈도우 와이파이 프로필 XML 양식에 맞추어 텍스트를 쓰고 스트리밍 에셋 폴더에 추가합니다.
byte[] ba = Encoding.Default.GetBytes(wifiName);
var hexString = BitConverter.ToString(ba);
hexString = hexString.Replace("-", "");
string wifiNameHex = hexString;
string addProfileXml =
$"<?xml version=\"1.0\"?>\n" +
$"<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\n" +
$" <name>{HMD_WIFI_NAME}</name>\n" +
$" <SSIDConfig>\n" +
$" <SSID>\n" +
$" <hex>{wifiNameHex}</hex>\n" +
$" <name>{HMD_WIFI_NAME}</name>\n" +
$" </SSID>\n" +
$" </SSIDConfig>\n" +
$" <connectionType>ESS</connectionType>\n" +
$" <connectionMode>auto</connectionMode>\n" +
$" <MSM>\n" +
$" <security>\n" +
$" <authEncryption>\n" +
$" <authentication>WPA2PSK</authentication>\n" +
$" <encryption>AES</encryption>\n" +
$" <useOneX>false</useOneX>\n" +
$" </authEncryption>\n" +
$" <sharedKey>\n" +
$" <keyType>passPhrase</keyType>\n" +
$" <protected>false</protected>\n" +
$" <keyMaterial>{wifiPassword}</keyMaterial>\n" +
$" </sharedKey>\n" +
$" </security>\n" +
$" </MSM>\n" +
$" <MacRandomization xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v3\">\n" +
$" <enableRandomization>false</enableRandomization>\n" +
$" </MacRandomization>\n" +
$"</WLANProfile>";
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(filePath));
if (!directoryInfo.Exists) directoryInfo.Create();
FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(fileStream, Encoding.UTF8);
writer.Write(addProfileXml);
writer.Close();
스트리밍 에셋 폴더의 WIFI Profile xml의 파일경로를 가지고 WIFI 프로필을 추가합니다.
//새로운 와이파이 프로파일을 추가하기
StartNetsh($"wlan add profile filename=\"{filePath}\"");
이상입니다.