[C#][共享网络] Netsh命令实现共享,并查询连接用户

来源:互联网 发布:mybatis 能被sql注入吗 编辑:程序博客网 时间:2024/06/06 00:18

这个封装很方便了,设置名称密码,一键即可共享。

缺点就是第一次共享要到网络中心设置网卡共享。

现在是用在我的软件FLYFI中,百度即可搜索到,欢迎大伙品尝!

全部原创,转载请注明出处!

public class FLYFI_SHARE_ADMIN    {        /// <summary>        /// 获取是否共享属性        /// </summary>        public bool IsShar { get; set; }        /// <summary>        /// 获取是否增强共享        /// </summary>        public bool IsStrengthen { get; set; }        /// <summary>        /// 设置、获取共享账户ID        /// </summary>        public string ShareSSID { get; set; }        /// <summary>        /// 设置、获取共享账户密码        /// </summary>        public string SharePassword { get; set; }        /// <summary>        /// 无线WiFi接口名称        /// </summary>        public string WifiInterface { get; set; }        /// <summary>        /// 开启共享        /// </summary>        public bool StartShare(out string message)        {            bool bFlag = false;            message = "wlan set hostednetwork mode=allow ssid=" + this.ShareSSID + " key=" + this.SharePassword;            message = this.RunCommand(message);                //调用Doc命令            string str = this.RunCommand("wlan start hostednetwork");            int StrInt = str.IndexOf("已启动承载网络");            if (StrInt != -1)            {                this.IsShar = true;                bFlag = true;            }            message = str + message;            return bFlag;        }        /// <summary>        /// 关闭共享,若增强开启则关闭增强        /// </summary>        public void StopShare()        {            if (this.IsStrengthen)            {                this.StopStrengthen();                            }            this.RunCommand("wlan stop hostednetwork");             //停止承载网络                                          this.RunCommand("wlan set hostednetwork mode=disallow");//网络共享设置为禁止              this.IsShar = false;        }        /// <summary>        /// 开启共享增强        /// </summary>        public void StartStrengthen()        {            string str;            if (this.WifiInterface == null)                this.GetWifiInterface();            str = "wlan set autoconfig enabled=no interface=" + this.WifiInterface;            this.RunCommand(str);            this.IsStrengthen = true;        }        /// <summary>        /// 关闭共享增强        /// </summary>        public void StopStrengthen()        {            if (this.WifiInterface == null)                this.GetWifiInterface();            this.RunCommand("wlan set autoconfig enabled=yes interface=" + this.WifiInterface);            this.IsStrengthen = false;        }        /// <summary>        /// 获取无线接口名称        /// </summary>        public void GetWifiInterface()        {            string str = "wlan show drivers";            str = this.RunCommand(str).Substring(7, 100);            str = str.Substring(0, str.IndexOf("\r\n"));            this.WifiInterface = str;        }                /// <summary>        /// 打开网络共享中心        /// </summary>        public void OpenNetworkCenter()        {            this.RunCommand_CMD("ncpa.cpl");        }        /// <summary>        /// Process类执行DOS命令        /// </summary>        /// <param name="command">执行的命令行</param>        /// <returns></returns>        private string RunCommand(string command)        {            string returnStr = null;            //實例一個Process類,啟動一個獨立進程            Process p = new Process();            //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:            p.StartInfo.FileName = "netsh.exe";  //設定程序名            p.StartInfo.Arguments = command;            //設定程式執行參數            p.StartInfo.Verb = "runas";            p.StartInfo.UseShellExecute = false;        //關閉Shell的使用            p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入            p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出            p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出            p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口            p.Start();                                  //啟動            returnStr = p.StandardOutput.ReadToEnd();     //赋值            p.Dispose();                                //释放资源            return returnStr;        //從輸出流取得命令執行結果        }        /// <summary>        /// Process类执行CMD DOS命令        /// </summary>        /// <param name="command">执行的命令行</param>        /// <returns></returns>        private string RunCommand_CMD(string command)        {            string returnStr = null;            //實例一個Process類,啟動一個獨立進程            Process p = new Process();            //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:            p.StartInfo.FileName = "cmd.exe";           //設定程序名            p.StartInfo.Arguments = "/c" + command;     //設定程式執行參數            p.StartInfo.UseShellExecute = false;        //關閉Shell的使用            p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入            p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出            p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出            p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口            p.Start();                                  //啟動            returnStr = p.StandardOutput.ReadToEnd();     //赋值            p.Dispose();                                //释放资源            return returnStr;        //從輸出流取得命令執行結果        }        /// <summary>        /// 获取用户列表Mac地址        /// </summary>        /// <returns></returns>        public List<string> GetShareMacList()        {            List<string> ShareList = new List<string>();            string str = null;            try            {                str = this.RunCommand("wlan show hostednetwork");//查询语句                str = str.Substring(str.IndexOf(" 客户端数"));                str = str.Substring(str.IndexOf(":") + 2);            }            catch (Exception)            {                str = "0";            }            int Pstr = Convert.ToInt32(str.Substring(0, 1));            //刷新用户数量            if (Pstr > 0)            {//判断当前用户数量是否大于0                for (int i = 0, fir = 11; i < Pstr; i++)                {//进行修改                    try                    {                        ShareList.Add(str.Substring(fir, 17).ToLowerInvariant());                        fir = fir + 42;          //增加值,跳转到第二个用户                        //, sec = 36                        //状态str.Substring(sec, 7));//添加到内存表                        //sec = fir + 25;                    }                    catch (Exception)                    { }                }            }            return ShareList;        }            }

全部原创,转载请注明出处!

0 0
原创粉丝点击