Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Tip/Trick

Creating Wifi HotSpot with the Help of CMD and C#

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
21 Sep 2013CPOL2 min read 75.4K   17   27
How to create a Wifi HotSpot with the help of CMD and C#

Introduction

There are many applications on the internet that can convert your laptop or computer into Wifi hotspot like Conectify. This is what this application will do but no advance features like sharing internet. However Creation is very simple just specify the SSID and a key and you are done!

How It Works

The basic principal is that the actual creation is hidden behind the DOS command that we have to issue this application is the front end (GUI version) of this irritating command issuing procedure. so without getting into mess of directly accessing the hardware and services, we can achieve our purpose.

Background

The basic background of DOS is good and also of basic knowledge of C# to implement the code.

Using the Code

Assuming you have created the project and in Windows Form there are two text boxes, two labels and a button, copy and paste the following code in the Click event handler of Button:

C#
string ssid = textBox1.Text, key = textBox2.Text;
            if (!connect)
            {
                if (textBox1.Text == null || textBox1.Text == "")
                {
                    MessageBox.Show("SSID cannot be left blank !", 
                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {

                    if (textBox2.Text == null || textBox2.Text == "")
                    {
                        MessageBox.Show("Key value cannot be left blank !", 
                        "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        if (key.Length >= 6)
                        {
                            Zedfi_Hotspot(ssid, key,true);
                            textBox1.Enabled = false;
                            textBox2.Enabled = false;
                            button1.Text = "Stop";
                            connect = true;
                        }
                        else
                        {
                            MessageBox.Show("Key should be more then or Equal to 6 Characters !", 
                            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            else
            {
                Zedfi_Hotspot(null, null, false);
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                button1.Text = "Start";
                connect = false;
            }

Following is the method. Just copy it in your project to use it.

C#
private void Zedfi_Hotspot(string ssid, string key,bool status)
       {
           ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
           processStartInfo.RedirectStandardInput = true;
           processStartInfo.RedirectStandardOutput = true;
           processStartInfo.CreateNoWindow = true;
           processStartInfo.UseShellExecute = false;
           Process process = Process.Start(processStartInfo);

           if (process != null)
           {
               if (status)
               {
                   process.StandardInput.WriteLine("netsh wlan set hostednetwork mode=allow
                   ssid=" + ssid + " key=" + key);
                   process.StandardInput.WriteLine("netsh wlan start hosted network");
                   process.StandardInput.Close();
               }
               else
               {
                   process.StandardInput.WriteLine("netsh wlan stop hostednetwork");
                   process.StandardInput.Close();
               }
           }
       }

One thing to remember these DOS commands work only if the application has admin privileges. If the application has no privileges, then the hotspot will not be created so to solve this problem, we have to check whether we have privileges or not. The following method is for this purpose:

C#
public static bool IsAdmin()
       {
           WindowsIdentity id = WindowsIdentity.GetCurrent();
           WindowsPrincipal p = new WindowsPrincipal(id);
           return p.IsInRole(WindowsBuiltInRole.Administrator);
       }

This method will return false or true accordingly. If this returned false then it means we have no privileges so we have to restart the application in admin mode to restart. The following code must be copied in your project

C#
public void RestartElevated()
       {
           ProcessStartInfo startInfo = new ProcessStartInfo();
           startInfo.UseShellExecute = true;
           startInfo.CreateNoWindow = true;
           startInfo.WorkingDirectory = Environment.CurrentDirectory;
           startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
           startInfo.Verb = "runas";
           try
           {
               Process p = Process.Start(startInfo);
           }
           catch
           {

           }

           System.Windows.Forms.Application.Exit();
       }

Now the last step is to create the form Load event where we actually want to check the privileges and do something accordingly copy the following code into form Load event

C#
if (!IsAdmin())
            {
                RestartElevated();
            }

Also it is desired that if we close the application, then the Hotspot should also be stopped for this. Just copy the following code into the form Closing event:

C#
Zedfi_Hotspot(null,null,false);
            Application.Exit();

Now all is done. Just build the project and enjoy!

Points of Interest

This hotspot created can be useful to create a network and for data sharing among the network but internet sharing is not applicable till now. Any suggestions will be welcome!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSource code please Pin
Member 8503334-May-21 9:30
Member 8503334-May-21 9:30 
PraiseSource Code Please Pin
Member 96873172-Dec-20 6:02
Member 96873172-Dec-20 6:02 
Questionwifi hotspot Pin
Member 1346369229-Nov-17 1:04
Member 1346369229-Nov-17 1:04 
QuestionI want to get connected users list with IPAddress, MAC and Device name Pin
Member 854409428-Jan-16 1:29
Member 854409428-Jan-16 1:29 
AnswerRe: I want to get connected users list with IPAddress, MAC and Device name Pin
Zain Ul Abidin18-Mar-16 1:26
Zain Ul Abidin18-Mar-16 1:26 
NewsSource Code Pin
Manav_Rachna9-Nov-15 4:04
Manav_Rachna9-Nov-15 4:04 
QuestionWifi hotspot sharing Pin
Pankaj Choudhury9-Jan-15 20:11
Pankaj Choudhury9-Jan-15 20:11 
AnswerRe: Wifi hotspot sharing Pin
Zain Ul Abidin10-Jan-15 8:20
Zain Ul Abidin10-Jan-15 8:20 
GeneralRe: Wifi hotspot sharing Pin
Sepehr Mohammad11-Mar-15 9:28
Sepehr Mohammad11-Mar-15 9:28 
GeneralRe: Wifi hotspot sharing Pin
Zain Ul Abidin24-Mar-15 0:01
Zain Ul Abidin24-Mar-15 0:01 
GeneralRe: Wifi hotspot sharing Pin
Sepehr Mohammad24-Mar-15 0:04
Sepehr Mohammad24-Mar-15 0:04 
I thought you were going to update the code and add the functionality to share Wifi internet with the newly created connection.
Questionsource code Pin
Member 1053376718-Nov-14 6:10
Member 1053376718-Nov-14 6:10 
AnswerRe: source code Pin
Zain Ul Abidin21-Nov-14 6:19
Zain Ul Abidin21-Nov-14 6:19 
GeneralRe: source code Pin
Member 1053376712-Dec-14 3:41
Member 1053376712-Dec-14 3:41 
GeneralRe: source code Pin
Manav_Rachna18-Nov-15 20:21
Manav_Rachna18-Nov-15 20:21 
GeneralRe: source code Pin
Zain Ul Abidin22-Nov-15 1:22
Zain Ul Abidin22-Nov-15 1:22 
QuestionCan you tell me how to add admin rights. Pin
Member 86666691-Sep-14 21:35
Member 86666691-Sep-14 21:35 
AnswerRe: Can you tell me how to add admin rights. Pin
Zain Ul Abidin5-Sep-14 21:58
Zain Ul Abidin5-Sep-14 21:58 
Generalthnkx Pin
Sikandar Ali23-Jan-14 3:34
Sikandar Ali23-Jan-14 3:34 
QuestionAny way to check status of network? Pin
Oleg Evans27-Dec-13 5:46
Oleg Evans27-Dec-13 5:46 
AnswerRe: Any way to check status of network? Pin
Zain Ul Abidin31-Dec-13 2:35
Zain Ul Abidin31-Dec-13 2:35 
SuggestionNeed to add/ customize: Pin
bluetoothfx17-Dec-13 8:56
bluetoothfx17-Dec-13 8:56 
Questionerror Pin
Chuy951018-Nov-13 15:44
Chuy951018-Nov-13 15:44 
AnswerRe: error Pin
Zain Ul Abidin19-Nov-13 9:19
Zain Ul Abidin19-Nov-13 9:19 
QuestionIP Address never assigned Pin
John C Wollner29-Oct-13 1:39
John C Wollner29-Oct-13 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.