我写了一个用SSID连接到wifi的代码,但我不明白为什么它不起作用

时间:2016-04-17 19:44:15

标签: c# wifi

这是一个表单应用程序。

我正在使用:

using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Principal;

这是我的代码:

    Process NewProcess = new Process();

    public Form1()
    {
        InitializeComponent();
        NewProcess.StartInfo.UseShellExecute = false;
        NewProcess.StartInfo.CreateNoWindow = true;
        NewProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    }

检查用户是否为管理员

    public bool IsUserAdmin()
    {
        bool isAdmin;
        try
        {
            WindowsIdentity user = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(user);
            isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch(UnauthorizedAccessException)
        {
            isAdmin = false;
        }
        catch(Exception)
        {
            isAdmin = false;
        }
        return isAdmin;

    }

停止播放

    public void StopBroadcasting()
    {
        NewProcess.StartInfo.FileName = "netsh";
        NewProcess.StartInfo.Arguments = "wlan Stop hostednetwork";
        try
        {
            using (Process execute = Process.Start(NewProcess.StartInfo))
            {
                execute.WaitForExit();
                SetLanDetails();
            }
        }
        catch
        {

        }
    }

设置局域网详细信息

    public void SetLanDetails()
    {
        NewProcess.StartInfo.FileName = "netsh";
        NewProcess.StartInfo.Arguments = "wlan set hostednetwork mode=allow ssid="+textBox1.Text+" key="+ textBox2.Text;
        try
        {
            using (Process execute = Process.Start(NewProcess.StartInfo))
            {
                execute.WaitForExit();
                StartBroadcasting();
            }
        }
        catch
        {

        }
    }

开始播放

    public void StartBroadcasting()
    {
        NewProcess.StartInfo.FileName = "netsh";
        NewProcess.StartInfo.Arguments = "wlan start hostendnetwork";
        try
        {
            using (Process execute = Process.Start(NewProcess.StartInfo))
            {
                execute.WaitForExit();
                buttonStart.Text = "Stop";
            }
        }
        catch
        {

        }
    }

停止流程

    public void StopProcess()
    {
        NewProcess.StartInfo.FileName = "netsh";
        NewProcess.StartInfo.Arguments = "wlan stop hostendnetwork";
        try
        {
            using (Process execute = Process.Start(NewProcess.StartInfo))
            {
                execute.WaitForExit();
            }
        }
        catch
        {

        }
    }

点击开始按钮

    private void buttonStart_Click(object sender, EventArgs e)
    {
        if(buttonStart.Text=="Start")
        {
            StopBroadcasting();
            buttonStart.Text = "Stop";
        }
        else
        {
            StopProcess();
            buttonStart.Text = "Start";
        }

    }

我无法理解为什么它不起作用。我忘记了什么吗?我没有找到理由。

0 个答案:

没有答案