C#:WiFi

写的一个简单启动关闭WiFi的类:具体如下

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace MyWifi
{
    public class WiFi
    {
        #region 单例

        //private static WiFi instance = null;
        //private static object objLock = new object();

        //private WiFi()
        //{ }

        //public static WiFi Singlon()
        //{
        //    if (instance == null)
        //    { 
        //        lock(objLock)
        //        {
        //            if (instance == null)
        //            {
        //                instance = new WiFi();
        //            }
        //        }
        //    }
        //    return instance;
        //}

        #endregion

        private static string executeCmd(string command)
        {
            Process process = new Process
            {
                StartInfo = { FileName = " cmd.exe ", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, CreateNoWindow = true }
            };
            process.Start();
            process.StandardInput.WriteLine(command);
            process.StandardInput.WriteLine("exit");
            process.WaitForExit();
            string str = process.StandardOutput.ReadToEnd();
            process.Close();
            return str;
        }

        /// <summary>
        /// 共享网络
        /// </summary>
        /// <param name="wifiName">WiFi名称</param>
        /// <param name="wifiPassword">WiFi密码(不少于8位)</param>
        /// <returns>"新建共享网络成功!"||"搭建失败,请重试!"</returns>
        public static string AllowWiFi(string wifiName, string wifiPassword)
        {
            string createWifiRet = "搭建失败,请重试!";
            try
            {
                string command = "netsh wlan set hostednetwork mode=allow ssid=" + wifiName.Trim() + " key=" + wifiPassword.Trim();
                string cmdExecRet = executeCmd(command);
                createWifiRet = cmdExecRet;
                if (((createWifiRet.IndexOf("承载网络模式已设置为允许") > -1) && (createWifiRet.IndexOf("已成功更改承载网络的 SSID。") > -1)) && (createWifiRet.IndexOf("已成功更改托管网络的用户密钥密码。") > -1))
                {
                   createWifiRet = "新建共享网络成功!";
                }
                return createWifiRet;
            }
            catch(Exception e)
            {
                return createWifiRet + "\n\r" + e.Message;
            }
        }

        /// <summary>
        /// 禁止共享网络
        /// </summary>
        /// <returns>disallowWifiRet = "禁止共享网络成功!"||"操作失败,请重试!"</returns>
        public static string DisallowWifi()
        {
            string disallowWifiRet = "操作失败,请重试!";
            try
            {
                string command = "netsh wlan set hostednetwork mode=disallow";
                if (executeCmd(command).IndexOf("承载网络模式已设置为禁止") > -1)
                {
                    disallowWifiRet = "禁止共享网络成功!";
                }
                return disallowWifiRet;
            }
            catch(Exception e)
            {
                return disallowWifiRet + "\n\r" + e.Message;
            }
        }

        /// <summary>
        /// 启动承载网络(WiFi)
        /// </summary>
        /// <returns>"已启动承载网络!"||"启动承载网络失败,请尝试新建网络共享!"</returns>
        public static string StartWiFi()
        {
            string startWifiRet = "启动承载网络失败,请尝试新建网络共享!";
            try
            {
                if (executeCmd("netsh wlan start hostednetwork").IndexOf("已启动承载网络") > -1)
                {
                    startWifiRet = "已启动承载网络!";
                }
                return startWifiRet;
            }
            catch(Exception e)
            {
                return startWifiRet + "\n\r" + e.Message;
            }
        }

        /// <summary>
        /// 停止承载网络(WiFi)
        /// </summary>
        /// <returns>"已停止承载网络!"||"停止承载网络失败!"</returns>
        public static string StopWiFi()
        {
            string stopWifiRet = "停止承载网络失败!";
            try
            {
                if (executeCmd("netsh wlan stop hostednetwork").IndexOf("已停止承载网络") > -1)
                {
                    stopWifiRet = "已停止承载网络!";
                }
                return stopWifiRet;
            }
            catch(Exception e)
            {
                return stopWifiRet + "\n\r" + e.Message;
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/shenchao/p/4756753.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值