zoukankan      html  css  js  c++  java
  • C#-WiFi共享

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    namespace wifiGX
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public int wifikg = 0;
            private void Form1_Load(object sender, EventArgs e)
            {
    
    
            }
            //cmd调用
            private  string InvokeCmd(string cmdArgs)
            {
                string Tstr = "";
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
    
                p.StandardInput.WriteLine(cmdArgs);
                p.StandardInput.WriteLine("exit");
                Tstr = p.StandardOutput.ReadToEnd();
                p.Close();
                return Tstr;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (okmima == 1 && okmingzi == 1)
                {
                    try
                    {
                        string mingling = "netsh wlan set hostednetwork mode=allow ssid=" + mingzi.Text + " key=" + mima.Text;
                        string str = InvokeCmd(mingling);
                        richTextBox1.Text = str;
                        int cg = 0;
                        for (int i = 0; i < str.Length - 2; i++)
                            if (str.Substring(i, 2) == "成功")
                                cg++;
                        if (cg == 2)
                        {
                            richTextBox1.Text = "承载网络模式已设置为允许。
    已成功更改承载网络的 SSID。
    已成功更改托管网络的用户密钥密码。
    ";
                        }
                        else
                        {
                            richTextBox1.Text = " 系统函数调用错误,可能原因是账号密码不规则(密码尽量只用数字和字母)";
                        }
    
    
                    }
                    catch
                    {
                        richTextBox1 .Text =" 系统函数调用错误,可能原因是账号密码不规则(密码尽量只用数字和字母)";
                    }
                }
                else
                {
                    MessageBox.Show("wifi名字或者密码长度不符合要求");
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    string mingling = "netsh wlan start hostednetwork";
                    string str = InvokeCmd(mingling);
                    int mk = 0;
                    for(int i = 0;i < str.Length - 7;i ++)  
                    {
                        if (str.Substring(i, 7) == "已启动承载网络")
                        {
                            mk = 1;
                            richTextBox1.Text = "已启动承载网络";
                            wifikg = 1;
                        }
                     }
                    if (mk == 0) richTextBox1.Text = "启动失败!";
                    else
                    {
                        if (wifikg == 0)
                        {
                            richTextBox2.Text = "wifi未开启,暂无设置信息!";
                            richTextBox3.Text = "wifi未开启,暂无状态信息!";
                        }
                        else
                            try
                            {
                                mingling = "netsh wlan show hostednetwork";
                                str = InvokeCmd(mingling);
                                int s1 = 0, s2 = 0, s = 0, s3 = 0;
                                for (int i = 0; i < str.Length - 6; i++)
                                {
                                    if (str.Substring(i, 6) == "承载网络设置")
                                        s1 = i;
                                    if (str.Substring(i, 6) == "承载网络状态")
                                        s2 = i;
                                    if (s2 != 0 && str.Substring(i, 1) == "
    ") s++;
                                    if (s == 7 && s3 == 0) s3 = i;
                                }
                                richTextBox2.Text = str.Substring(s1, s2 - s1);
                                richTextBox3.Text = str.Substring(s2, s3 - s2);
                            }
                            catch
                            {
                                richTextBox2.Text = "信息更新失败!";
                                richTextBox3.Text = "信息更新失败!";
                            }
                    }
                }
                catch
                {
                    richTextBox1.Text = "启动失败!";
                }
    
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                //已停止承载网络
                try
                {
                    string mingling = "netsh wlan stop hostednetwork";
                    string str = InvokeCmd(mingling);
                    int mk = 0;
                    for (int i = 0; i < str.Length - 7; i++)
                    {
                        if (str.Substring(i, 7) == "已停止承载网络")
                        {
                            mk = 1;
                            richTextBox1.Text = "已停止承载网络";
                            wifikg = 0;
                        }
                    }
                    if (mk == 0) richTextBox1.Text = "停止失败!";
                }
                catch
                {
                    richTextBox1.Text = "停止失败!";
                }
            }
            int okmingzi = 0;
            int okmima = 0;
            private void mima_TextChanged(object sender, EventArgs e)
            {
                if (mima.Text.Length >= 8 && mima.Text.Length <= 12)
                {
                    label1.Text = "Yes";
                    label1.ForeColor = Color.Green;
                    okmima  = 1;
                }
                else
                {
                    label1.Text = "No";
                    label1.ForeColor = Color.Red ;
                    okmima  = 0;
                }
            }
    
            private void mingzi_TextChanged(object sender, EventArgs e)
            {
                if (mingzi.Text.Length >= 1 && mingzi.Text.Length <= 10)
                {
                    okmingzi = 1;
                    label2.Text = "Yes";
                    label2.ForeColor = Color.Green;
                }
                else
                {
                    okmingzi = 0;
                    label2.Text = "No";
                    label2.ForeColor = Color.Red;
                }
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if(wifikg == 1)
                try
                {
                    string mingling = "netsh wlan stop hostednetwork";
                    string str = InvokeCmd(mingling);
                    int mk = 0;
                    for (int i = 0; i < str.Length - 7; i++)
                    {
                        if (str.Substring(i, 7) == "已停止承载网络")
                        {
                            mk = 1;
                            richTextBox1.Text = "已停止承载网络";
                        }
                    }
                    if (mk == 0) richTextBox1.Text = "停止失败!";
                }
                catch
                {
                    richTextBox1.Text = "停止失败!";
                }
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (wifikg == 0)
                {
                    richTextBox2.Text = "wifi未开启,暂无设置信息!";
                    richTextBox3.Text = "wifi未开启,暂无状态信息!";
                }
                else
                    try
                    {
                        string mingling = "netsh wlan show hostednetwork";
                        string str = InvokeCmd(mingling);
                        int s1 = 0, s2 = 0, s = 0, s3 = 0;
                        for (int i = 0; i < str.Length - 6; i++)
                        {
                            if (str.Substring(i, 6) == "承载网络设置")
                                s1 = i;
                            if (str.Substring(i, 6) == "承载网络状态")
                                s2 = i;
                            if (s2 != 0 && str.Substring(i, 1) == "
    ") s++;
                            if (s == 7 && s3 == 0) s3 = i;
                        }
                        richTextBox2.Text = str.Substring(s1, s2 - s1);
                        richTextBox3.Text = str.Substring(s2, s3 - s2);
                    }
                    catch
                    {
                        richTextBox2.Text = "信息更新失败!";
                        richTextBox3.Text = "信息更新失败!";
                    }
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                Form2 f = new Form2();
                f.Show();
            }
        }
    }
    

  • 相关阅读:
    POJ 3259 Wormholes【BellmanFord】
    POJ 2960 SNim【SG函数的应用】
    ZOJ 3578 Matrixdp水题
    HDU 2897 邂逅明下【bash博弈】
    BellmanFord 算法及其优化【转】
    【转】几个Java的网络爬虫
    thinkphp 反字符 去标签 自动加点 去换行 截取字符串 冰糖
    php 二维数组转 json文本 (jquery datagrid 数据格式) 冰糖
    PHP 汉字转拼音(首拼音,所有拼音) 冰糖
    设为首页与加入收藏 兼容firefox 冰糖
  • 原文地址:https://www.cnblogs.com/csnd/p/12062160.html
Copyright © 2011-2022 走看看