写的一个简单启动关闭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("承载网络模式已设置为允许") > -) && (createWifiRet.IndexOf("已成功更改承载网络的 SSID。") > -)) && (createWifiRet.IndexOf("已成功更改托管网络的用户密钥密码。") > -))
{
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("承载网络模式已设置为禁止") > -)
{
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("已启动承载网络") > -)
{
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("已停止承载网络") > -)
{
stopWifiRet = "已停止承载网络!";
}
return stopWifiRet;
}
catch(Exception e)
{
return stopWifiRet + "\n\r" + e.Message;
}
}
}
}

最新文章

  1. mysql数据库引擎 MyISAM和 InnoDB区别
  2. 新冲刺Sprint3(第六天)
  3. iOS开发——UI基础-Xcode资源拷贝
  4. php获取textarea的值并处理回车换行的方法
  5. ElasticSearch Filter Aggregations
  6. 开发流程习惯的养成—TFS简单使用
  7. 一 JavaScript应用开发实践指南
  8. SVN的CheckOut操作和Export操作的区别
  9. ArrayList与Vector的区别
  10. 通过slave_exec_mode=IDEMPOTENT跳过主从复制中的错误
  11. SSM框架开发遇到的问题
  12. Python3.6 连接MySQL(二)转载
  13. zeros()和ones()和eye()
  14. linux下批量查找UTF-8的BOM文件,并去除BOM
  15. 【angularJS】过滤器
  16. 百度富文本编辑器整合fastdfs文件服务器上传
  17. RXD, tree and sequence IN HDU6065
  18. TCP/IP与OSI模型
  19. gcc gdb调试 &amp; 命令行带参 (一) ******
  20. mutex锁住共用线程函数 造成了死锁 ,为什么?

热门文章

  1. 解决:AppMsg - Warning: calling DestroyWindow in CWnd::~CWnd; OnDestroy or PostNcDestroy in derived class will not be called
  2. cocos2dx 3.x(点击屏幕移动精灵,拖动精灵)不需要写回调函数Lua表达式
  3. 查看python的路径
  4. 微信成为开发者C#代码
  5. 原来现在很多人都用SignalR来实现Chat Room
  6. J2EE MyBatis使用
  7. Leetcode: Max Sum of Rectangle No Larger Than K
  8. Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
  9. Lintcode: Median
  10. SQL封装、多态与重载